c++17

Using a constexpr static member of a reference as template argument

让人想犯罪 __ 提交于 2019-12-19 17:41:51
问题 I'm trying to figure out whether GCC or Clang interpret the C++17 standard differently / wrong here. This is my code, which does compile using GCC 8, but not using Clang 6: struct BoolHolder { constexpr static bool b = true; }; template<bool b> class Foo {}; int main() { BoolHolder b; Foo<b.b> f; // Works BoolHolder & br = b; Foo<br.b> f2; // Doesn't work } I wonder why that is. Obviously, b.b is a valid constexpr (or the first Foo<b.b> wouldn't be valid). Is br.b not a valid constexpr? Why?

Using a constexpr static member of a reference as template argument

雨燕双飞 提交于 2019-12-19 17:41:10
问题 I'm trying to figure out whether GCC or Clang interpret the C++17 standard differently / wrong here. This is my code, which does compile using GCC 8, but not using Clang 6: struct BoolHolder { constexpr static bool b = true; }; template<bool b> class Foo {}; int main() { BoolHolder b; Foo<b.b> f; // Works BoolHolder & br = b; Foo<br.b> f2; // Doesn't work } I wonder why that is. Obviously, b.b is a valid constexpr (or the first Foo<b.b> wouldn't be valid). Is br.b not a valid constexpr? Why?

Understand structured binding in C++17 by analogy

梦想的初衷 提交于 2019-12-19 17:13:45
问题 I'm trying to understand structured binding introduced in C++17. The explanation on cppreference is not obvious to me, but it looks like cv-auto ref-operator [x, y, z] = ... is roughly equivalent to (not to consider array case) cv-auto ref-operator unique_name = ... #define x unique_name.member_a #define y unique_name.member_b #define z unique_name.member_c The key point here is that x y z are not independently defined variables, but just aliases of the return value members. And cv-auto ref

C++ concept with friend-like access

廉价感情. 提交于 2019-12-19 17:13:10
问题 Is it possible to make this code work as I'd like? I.e. to allow the concept to have access to a private member funcion? template <typename T> concept bool Writeable() { return requires (T x,std::ostream os) { { x.Write(os) } -> void }; } template <Writeable T> void Write(std::ostream &os,const T &x) { x.Write(os); } class TT { private: void Write(std::ostream &os) const { os << "foo"; } //friend concept bool Writeable<TT>(); friend void ::Write<TT>(std::ostream &,const TT &); }; Thanks 回答1:

Understand structured binding in C++17 by analogy

一曲冷凌霜 提交于 2019-12-19 17:13:10
问题 I'm trying to understand structured binding introduced in C++17. The explanation on cppreference is not obvious to me, but it looks like cv-auto ref-operator [x, y, z] = ... is roughly equivalent to (not to consider array case) cv-auto ref-operator unique_name = ... #define x unique_name.member_a #define y unique_name.member_b #define z unique_name.member_c The key point here is that x y z are not independently defined variables, but just aliases of the return value members. And cv-auto ref

Strange MSVC behaviour with std::experimental::is_detected

ぐ巨炮叔叔 提交于 2019-12-19 10:17:42
问题 I implemented std::experimental::is_detected based on this article on cppreference.com (Part of the code is below + working repro). It works well on G++ and Clang++, but results in strange errornous behaviour with MSVC: is_detected seems to always be bool_constant<true> ! Here you can see the correct result using gcc 5.x : @ideone.com But with MSVC 19 (shipped with VS2015) the tests always succeed: Z:\>cl /EHsc test.cxx .... Z:\>test true, true So, is this a known bug in the compiler? Is it

Are pointers to allocated memory outside object's lifetime “invalid pointer[s]” or “pointer[s] to an object”?

这一生的挚爱 提交于 2019-12-19 09:25:00
问题 C++17 (draft N4659) [basic.compound]/3 says: Every value of pointer type is one of the following: a pointer to an object or function (the pointer is said to point to the object or function), or a pointer past the end of an object ([expr.add]), or the null pointer value ([conv.ptr]) for that type, or an invalid pointer value. To which of these categories belong pointers to allocated memory outside the lifetime of objects, specifically the values of a at // (1) through // (3) and b at // (4) in

Template argument deduction for class templates in C++17: am I doing it wrong?

非 Y 不嫁゛ 提交于 2019-12-19 09:14:20
问题 According to https://gcc.gnu.org/projects/cxx-status.html, version 7 of g++, used with flag -std=c++1z , supports template argument deduction for class templates. I would expect the following code to compile, especially as Base is an abstract class, therefore: 1. the compiler knows no instance of Base can be created; 2. the pointer to base pt_base points to a clearly defined instance (i.e. Derived<int>{42} ) where the type ( int ) is explicit. template<typename ValueType> class Base { public:

Using `void_t` to detect multiple inheritance type repetition errors

这一生的挚爱 提交于 2019-12-19 05:54:37
问题 I want to implement a has_no_duplicates<...> type trait that evaluates to std::true_type if the passed variadic type list has no duplicate types. static_assert(has_no_duplicates<int, float>{}, ""); static_assert(!has_no_duplicates<float, float>{}, ""); Let's assume, for the scope of this question, that I want to do that using multiple inheritance. When a class inherits from the same type more than once, an error occurs. template<class T> struct type { }; template<class... Ts> struct dup

Lambda as default argument fails

倾然丶 夕夏残阳落幕 提交于 2019-12-19 05:07:41
问题 I get an error with the latest versions of clang and gcc with this code: int main() { auto lambda = [] (auto = [] {}) {}; lambda(); } Clang gives the error: prog.cc: In function 'int main()': prog.cc:3:12: error: no match for call to '(main()::<lambda(auto:1)>) ()' lambda(); ^ prog.cc:2:35: note: candidate: template<class auto:1> main()::<lambda(auto:1)> auto lambda = [] (auto = [] {}) {}; ^ prog.cc:2:35: note: template argument deduction/substitution failed: prog.cc:3:12: note: couldn't