c++17

Static member access in constant expressions

和自甴很熟 提交于 2019-12-01 15:29:52
问题 Accessing static class member functions or variables, can be done in two ways: through an object ( obj.member_fun() or obj.member_var ) or through the class ( Class::member_fun() or Class::member_var ). However, in constexpr functions, Clang gives an error on the object access and requires to use class access: struct S { constexpr static auto s_v = 42; constexpr static auto v() { return s_v; } }; #define TEST 1 constexpr auto foo(S const& s [[maybe_unused]]) { #if TEST constexpr auto v = s.v(

constexpr function with unused reference argument – gcc vs clang

▼魔方 西西 提交于 2019-12-01 15:18:11
问题 Consider the following code: template <int N, typename T> void f(T) { } template <typename T> constexpr int k(T&) { return 0; } int main() { constexpr auto i = 1; f<k(i)>([&i] { f<k(i)>(0); }); } clang++ (trunk) compiles it. g++ (trunk) fails with the following error: <source>: In lambda function: <source>:11:19: error: no matching function for call to 'f<k<const int>((* & i))>(int)' 11 | f<k(i)>(0); | ^ <source>:1:35: note: candidate: 'template<int N, class T> void f(T)' 1 | template <int N,

Shift operands sequenced in C++17

大憨熊 提交于 2019-12-01 15:12:44
I read in the C++17 Standard $8.5.7.4: The expression E1 is sequenced before the expression E2. for shift operators. Also cppreference rule 19 says: In a shift operator expression E1<<E2 and E1>>E2, every value computation and side-effect of E1 is sequenced before every value computation and side effect of E2 But when I try to compile the following code with gcc 7.3.0 or clang 6.0.0 #include <iostream> using namespace std; int main() { int i = 5; cout << (i++ << i) << endl; return 0; } I get the following gcc warning: ../src/Cpp_shift.cpp: In function ‘int main()’: ../src/Cpp_shift.cpp:6:12:

What is the purpose of std::forward()'s rvalue reference overload?

会有一股神秘感。 提交于 2019-12-01 15:04:05
I'm experimenting with Perfect Forwarding and I found that std::forward() needs two overloads: Overload nr. 1: template <typename T> inline T&& forward(typename std::remove_reference<T>::type& t) noexcept { return static_cast<T&&>(t); } Overload nr.2: template <typename T> inline T&& forward(typename std::remove_reference<T>::type&& t) noexcept { static_assert(!std::is_lvalue_reference<T>::value, "Can not forward an rvalue as an lvalue."); return static_cast<T&&>(t); } Now a typical scenario for Perfect Forwarding is something like template <typename T> void wrapper(T&& e) { wrapped(forward<T>

What are the types of identifiers introduced by structured bindings in C++17?

梦想的初衷 提交于 2019-12-01 14:57:34
问题 To my knowledge, identifiers introduced by structured bindings in C++17 are in fact references to some "hidden" variable. Such that auto [ a, b ] = std::make_tuple(1, 2); is kind-of equivalent to auto e = std::make_tuple(1, 2); auto& a = std::get<0>(e); auto& b = std::get<1>(e); However, if I print out std::is_reference<decltype(a)>::value , I get 0 in the first case 1 in the second. Why is that? 回答1: if I print out std::is_reference<decltype(a)>::value , I get 0 in the first case 1 in the

Structured binding and tie()

喜夏-厌秋 提交于 2019-12-01 14:38:10
问题 Given these declarations: int a[3] {10,20,30}; std::tuple<int,int,int> b {11,22,33}; I can use structured binding declarations to decode a and b : auto [x1,y1,z1] = a; auto [x2,y2,z2] = b; But if x1 , y1 , etc. already exist, what do I do? std::tie(x1,y1,z1) = a; // ERROR std::tie(x2,y2,z2) = b; // OK This works for b but not for a . Is there a similar simple construct that works for a , or do I have to fetch a[0] , a[1] and a[2] separately? 回答1: Nope. Structured bindings has specific

std::visit for variant fails to compile under clang 5 [duplicate]

拥有回忆 提交于 2019-12-01 14:26:20
问题 This question already has an answer here : get<string> for variants fail under clang++ but not g++ (1 answer) Closed last year . The following uses of std::visit compiles properly under gcc 7.2 but fails to compile under clang 5.0. Does anyone know what the problem is? #include <variant> struct S1 {int foo() { return 0; }}; struct S2 {int foo() { return 1; }}; using V = std::variant<S1, S2>; int bar() { V v; return std::visit([](auto& s) { return s.foo(); }, v); } The first error is this:

Persist C++ type info to file for use across program invocations

一曲冷凌霜 提交于 2019-12-01 14:10:01
Edit: highlighting the actual question with more context available if desired. I want to implement the following method: template <typename T> <unspecified> type_identification(); For a generic type T, it must return a (relatively) unique identification that is stable over multiple invocations of the same program and may be used for inter-process communication (so no pointer-based solutions). Compiler-specific macros/extensions/intrinsics may be used, preferably available for both MSVC and clang. I have considered std::type_info::hash_code , or std::type_info::name but both of those cannot

Shift operands sequenced in C++17

て烟熏妆下的殇ゞ 提交于 2019-12-01 14:07:42
问题 I read in the C++17 Standard $8.5.7.4: The expression E1 is sequenced before the expression E2. for shift operators. Also cppreference rule 19 says: In a shift operator expression E1<<E2 and E1>>E2, every value computation and side-effect of E1 is sequenced before every value computation and side effect of E2 But when I try to compile the following code with gcc 7.3.0 or clang 6.0.0 #include <iostream> using namespace std; int main() { int i = 5; cout << (i++ << i) << endl; return 0; } I get

What is the purpose of std::forward()'s rvalue reference overload?

偶尔善良 提交于 2019-12-01 13:53:59
问题 I'm experimenting with Perfect Forwarding and I found that std::forward() needs two overloads: Overload nr. 1: template <typename T> inline T&& forward(typename std::remove_reference<T>::type& t) noexcept { return static_cast<T&&>(t); } Overload nr.2: template <typename T> inline T&& forward(typename std::remove_reference<T>::type&& t) noexcept { static_assert(!std::is_lvalue_reference<T>::value, "Can not forward an rvalue as an lvalue."); return static_cast<T&&>(t); } Now a typical scenario