c++14

void_t in parameter list works but not as return type

拥有回忆 提交于 2019-12-05 13:39:22
There's an example on cppreference about the using alias. This example fails because int has no member foo : template<typename...> using void_t = void; template<typename T> void_t<typename T::foo> f(); f<int>(); // error, int does not have a nested type foo This is clear, but when I tried putting the void_t part in the parameter list it unexpectedly compiled: template<typename...> using void_t = void; template<typename T> void f(void_t<typename T::foo>); f<int>(); It compiles on clang but not in gcc. Is this a bug? template<class...>struct voider{using type=void;}; template<class...Ts>using

What is difference between decltype(auto) and decltype(returning expr) as return type?

£可爱£侵袭症+ 提交于 2019-12-05 13:39:09
问题 What is the difference between decltype(auto) and decltype(returning expression) as return type of a function (template) if expr used without parentheses in both cases? auto f() -> decltype(auto) { return expr; } // 1 auto f() -> decltype(expr) { return expr; } // 2 Above f can be defined/declared in any context and can be either (member) function or (member) function template, or even (generic) lambda. expr can depend on any template parameters. In second version both expr are exactly the

User-declared default constructor + in-class initializers != user-provided constructor? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-05 13:01:34
问题 This question already has answers here : Why does C++ require a user-provided default constructor to default-construct a const object? (5 answers) Closed 2 years ago . The Clang documentation neatly explains that If a class or struct has no user-defined default constructor, C++ doesn't allow you to default construct a const instance of it like this ([dcl.init], p9) The rationale being that if a const object is not correctly initialized, it cannot be changed later on. The following code has

Segmentation fault on gcc caused by lambda wrapper over variadic template function call

醉酒当歌 提交于 2019-12-05 13:00:47
问题 I've spent quite a few hours today trying to understand why this code segfaults on g++6.2 and g++7.0 , while happily working as intended on clang++3.9 (and 4.0 ) . I reduced the issue to a 85 lines self-contained code snippet, which does not segfault upon normal execution, but always reports an error under UBSAN. The issue is reproducible on wandbox, by compiling with g++7 , enabling optimizations and passing -fsanitize=undefined as an extra flag. This is what UBSAN reports: prog.cc: In

Given that p is a pointer is “p > nullptr” well-formed?

亡梦爱人 提交于 2019-12-05 12:24:50
问题 Given a pointer p : char *p ; // Could be any type assuming p is properly initialized is the following well-formed: if (p > 0) // or p > nullptr More generally is it well-formed to use a relational operator when one operand is a pointer and the other is a null pointer constant? 回答1: In C++14 this code is ill-formed but prior to the C++14 this was well-formed code( but the result is unspecified ), as defect report 583: Relational pointer comparisons against the null pointer constant notes: In

Constexpr find implementation

≯℡__Kan透↙ 提交于 2019-12-05 12:16:12
After answering this question and reading this talk and looking at this code , I want to implement constexpr find with just simple array class. Consider following example: #include <cstddef> template <class It, class T> constexpr auto constexpr_find(const It& b, const It& e, T value) { auto begin = b; while (begin != e) { if (*begin == value) break; ++begin; } return *begin; } template<typename T, size_t N> class array { public: typedef T* iterator; typedef const T* const_iterator; constexpr auto begin() const { return const_iterator(array_); } constexpr auto end() const { return const

constexpr reference to non-const object

爷,独闯天下 提交于 2019-12-05 12:12:43
Is it permitted to declare a non-const reference as constexpr ? Example code: int x = 1; constexpr int& r = x; This is accepted by gcc and clang (I tried several current and past versions of both, back to C++11, and all accepted it). However I think it should not be accepted because C++14 [dcl.constexpr/9] says: if a constexpr specifier is used in a reference declaration, every full- expression that appears in its initializer shall be a constant expression and x is not a constant expression. The language in the latest C++17 draft of [dcl.constexpr] changed and doesn't even mention constexpr

How to fill array with contents of a template parameter pack?

≡放荡痞女 提交于 2019-12-05 11:52:42
I had nested partially specialized template code working with VS 2015 until I discovered that it was not standards-compliant . I want it to be so I twisted my code to overcome the former issue and also that one and have now hit a hard wall. Using variadic templates and partial specialization I would like to fill an array at compile-time given a fixed set of parameters. What I want to achieve also seems similar to this answer but I did not manage to make it work. Consider the following program: #include <cstdlib> template <typename T, std::size_t Size> struct Array; template <typename T, std:

Buffer overrun with bit-fields and value initialization - compiler bug or undefined behavior?

孤者浪人 提交于 2019-12-05 11:52:22
问题 So, I encountered a weird bug at work when I was trying to reduce the size of a struct by using bit-fields. I managed to isolate the problem and produce a minimal version that replicates the issue. This had the added benefit that MSVC now even warns about what it is going to do: Link to Compiler Explorer. However, Clang and GCC have no problems with this code. #include <new> enum foo : unsigned char { zero, one }; struct S { int a{ 42 }; //Intentionally not trivial foo b : 1; foo c : 1; };

Variable templates + generic lambdas for std::map

烂漫一生 提交于 2019-12-05 11:11:34
An answer to C++14 Variable Templates: what is the purpose? Any usage example? proposes a usage example of variable templates + generic lambdas that would look something like this: void some_func() { template<typename T> std::map<int, T> storage; auto store = []<typename T>(int key, const T& value) { storage<T>.insert(key, value) }; store(0, 2); store(1, "Hello"s); store(2, 0.7); // All three values are stored in a different map, according to their type. } Unfortunately it doesn't compile so I've tried to "fix" it and this is my attempt so far. #include <map> template<typename T> std::map<int,