c++14

Specialize same operator for different traits

杀马特。学长 韩版系。学妹 提交于 2019-12-07 11:03:08
问题 I want to do the following with specialization by traits. Array Aa = Scalar in_a would use overload I . Array Aa = Array Bb would use overload II . In the following code, overload II never get used. Someone mentioned that T1 cannot be deduced in overload II . How to fix that? I used the C++ shell to compile the code with C++14. #include <iostream> #include <type_traits> using namespace std; class A; // forward declaration. template <typename T> struct is_A : false_type {}; template <> struct

void_t in parameter list works but not as return type

房东的猫 提交于 2019-12-07 10:46:59
问题 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

c++ non-type parameter pack expansion

白昼怎懂夜的黑 提交于 2019-12-07 09:22:51
问题 I am writing template function that is parametrized by single type, and has variable number of parameters of the same type (not of different types). It should check if first value is among the rest. I wanted to write it like this: #include <unordered_set> template <typename T> static bool value_in(T val, T vals...) { // compiles, but uses only vals[0]: const std::unordered_set<T> allowed {vals}; // error: pack expansion does not contain any unexpanded parameter packs: // const std::unordered

C++ default initialization types [duplicate]

半腔热情 提交于 2019-12-07 09:22:39
问题 This question already has answers here : What do the following phrases mean in C++: zero-, default- and value-initialization? (2 answers) Closed 2 years ago . Why those two scenarios (the initialization of A and of C ) produce different default initialization results in C++ 14? I can't understand the result based on the default initialization rules in cppreference.com struct A { int m; }; struct C { C() : m(){}; int m; }; int main() { A *a, *d; A b; A c{}; a=new A(); d=new A; cout<<a->m<<endl

Are there cases in which trailing-return-type syntax in lambda cannot be avoided?

笑着哭i 提交于 2019-12-07 09:13:30
问题 In relation to a previous question (Is it possible to return an object of type T by reference from a lambda without using trailing return type syntax?), I was wondering if there is any other significant case or example in which trailing-return-type syntax, when using lambdas, can not be avoided. 回答1: In C++14, a bit contrived example is the use of sfinae in combination with a generic lambda: [](auto &&arg) -> decltype(arg.f(), void()) { /* do whatever you want */ } Anyway one could argue that

Should trivial default constructor respect default member initializer here?

邮差的信 提交于 2019-12-07 06:40:27
问题 Consider the code: #include <atomic> #include <iostream> struct stru { int a{}; int b{}; }; int main() { std::atomic<stru> as; auto s = as.load(); std::cout << s.a << ' ' << s.b << std::endl; } Note that although stru has default member initializer, it still qualifies as an aggregate type since C++14. std::atomic has a trivial default constructor. According to the standard, should the members of as be initialized to zero? clang 6.0.0 doesn't do this (see here), while gcc 7.2.0 seems so (see

Constexpr find implementation

这一生的挚爱 提交于 2019-12-07 06:22:56
问题 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

How can I use `std::array` for a template parameter of the form `template<typename> class`?

↘锁芯ラ 提交于 2019-12-07 06:13:50
问题 Please consider the following tree class template<typename T, template<typename> class Tuple> class tree { private: T m_value; Tuple<tree> m_children; }; template<typename T, std::size_t N> using static_tree = tree<T, std::array<T, N>>; which is not well-defined. std::array<T, N> is not a suitable template parameter for Tuple . I assume the intend of static_tree is clear. We could do something like template<std::size_t N> struct helper { template<typename T> using type = std::array<T, N>; };

Insert/remove type into variadic template list (parameter pack)

陌路散爱 提交于 2019-12-07 05:51:19
问题 What is the best way of implementing index-based insertion and deletion of a type in a variadic template type list (parameter pack)? Desired code/behavior: template<typename...> struct List { /* ... */ }; static_assert(is_same < List<int, char, float>::Insert<int, 0>, List<int, int, char, float> >()); static_assert(is_same < List<int, char, float>::Insert<int, 2>, List<int, char, int, float> >()); static_assert(is_same < List<int, char, float>::Remove<0>, List<char, float> >()); static_assert

How to make static_assert play nice with SFINAE

偶尔善良 提交于 2019-12-07 05:00:33
问题 Update I posted a working rough draft of rebind as an answer to the question. Though I didn't have much luck finding a generic way to keep static_assert s from breaking metafunctions. Basically I want to check if a templated type T<U, Args...> can be constructed from some other type T<V, Args...> . Where T and Args... is the same in both types. The problem is, T<> might have a static_assert in it that totally breaks my metafunction. Below is a rough summary of what I'm trying to do. template