c++14

Self-document a type-alias (typedef) to indicate that it will be used in another certain class

只谈情不闲聊 提交于 2019-12-22 14:18:58
问题 How to self-document a type-alias that is used in another certain library? In the below example, class User defines an alias User::type that is supposed to be referred only in class Library via T::type . Here is the diagram :- Library.h Library<T> expected only T that defines a certain alias (e.g. T::type in this example). #include <iostream> class Base{}; //dummy for the sake of example template<class T>class Library{ Base* t=nullptr; public: typename T::type getValue(){return static_cast

Self-document a type-alias (typedef) to indicate that it will be used in another certain class

不打扰是莪最后的温柔 提交于 2019-12-22 14:18:15
问题 How to self-document a type-alias that is used in another certain library? In the below example, class User defines an alias User::type that is supposed to be referred only in class Library via T::type . Here is the diagram :- Library.h Library<T> expected only T that defines a certain alias (e.g. T::type in this example). #include <iostream> class Base{}; //dummy for the sake of example template<class T>class Library{ Base* t=nullptr; public: typename T::type getValue(){return static_cast

How to iterate over std::index_sequence

旧街凉风 提交于 2019-12-22 09:49:00
问题 I have this code in my source: template <std::size_t... Dims> class DimensionPack { public: using Dimensions = std::index_sequence<Dims...>; static const std::size_t total_dimensions = sizeof...(Dims); std::vector<unsigned int> even_or_odd; public: DimensionPack() { unsigned idx = 0; for ( ; idx < total_dimensions; idx++ ) { //MatrixDimensionOddOrEven mdoe( Dimensions[idx] ); //unsigned int val = mdoe.even_or_odd; //even_or_odd.push_back( val ); } } }; The commented lines of code is the code

Template specialisation with default argument [duplicate]

血红的双手。 提交于 2019-12-22 09:29:43
问题 This question already has answers here : How does `void_t` work (2 answers) Closed last year . I have a program that is as follows. There is a base template struct X and a partial specialisation with SFINAE. template <typename T, typename U = void> struct X{ X() { std::cout << "in 1" << std::endl; }; }; template <typename T> struct X< T, std::enable_if_t<std::is_integral_v<T>> > { X() { std::cout << "in 2" << std::endl; }; }; int main() { X<int> x; } When running the program in 2 is printed.

auto parameter type in functions

你说的曾经没有我的故事 提交于 2019-12-22 09:29:22
问题 I would like to know if the standard committee considered expanding the C++14 auto keyword to deduce function template parameter type, as it exists today in generic lambdas. (as can be seen nicely depicted in this answer) Because it works in lambda functions, it should also work in any function. Of course it would be totally redundant with the classic syntax: template< typename T > void f(T param); But being able to write this, for the same result: void f(auto param); I think would allow for

Conditionally constexpr member function

岁酱吖の 提交于 2019-12-22 08:57:30
问题 Suppose I have a template class template <typename T> class foo { T m; decltype(auto) f() { return m.f(); } }; How can I give foo:f() the constexpr specifier only if T::f() is constexpr? 回答1: You just slap a constexpr on it: constexpr decltype(auto) f() { return m.f(); } Yes, it's perfectly still valid even if T::f() isn't constexpr ; such a function simply can't be used in constant expressions. See [dcl.constexpr]/6. 来源: https://stackoverflow.com/questions/41517603/conditionally-constexpr

How to access n-th value of an integer_sequence? [duplicate]

狂风中的少年 提交于 2019-12-22 08:55:16
问题 This question already has answers here : template parameter packs access Nth type and Nth element (5 answers) Closed last year . I would like to know how to access the n-th value of an std::integer_sequence . For example given a type using foo = std::integer_sequence<int, 3, 1, 4>; I would like to have something like auto i = get<foo, 2>(); // i = 4 Is there something in the standard library to do that? If not, do I need to resort to an iterative solution if I want this to work in C++14 (not

How to access n-th value of an integer_sequence? [duplicate]

喜你入骨 提交于 2019-12-22 08:54:44
问题 This question already has answers here : template parameter packs access Nth type and Nth element (5 answers) Closed last year . I would like to know how to access the n-th value of an std::integer_sequence . For example given a type using foo = std::integer_sequence<int, 3, 1, 4>; I would like to have something like auto i = get<foo, 2>(); // i = 4 Is there something in the standard library to do that? If not, do I need to resort to an iterative solution if I want this to work in C++14 (not

Taylor series expansion as constexpr

一个人想着一个人 提交于 2019-12-22 08:47:33
问题 I'm trying to build a simple sine function using taylor series expansion that can be evaluated at compile time using C++14 constexpr . My code is compiling, but the compiler doesn't generate a constant. sine is defined as follows: template <int P, typename T = double> constexpr T sine(T x) { T result = x; for (int i = 1; i < P; ++i) result += power<T>(-1, i) * power<T>(x, 1 + 2 * i) / factorial<T>(1 + 2 * i); return result; } I can provide code for power and factorial if needed. They are

Can't get C++14 using XCode 7.0.1

☆樱花仙子☆ 提交于 2019-12-22 08:27:57
问题 I'm trying to use C++14 in XCode 7.0.1 and it's not working. I'm trying to use a std::string literal and I'm getting the error: Invalid suffix on literal; C++11 requires a space between literal and identifier. Since this is supposed to use C++14 why am I getting this error? I tried looking all around XCode for a setting or something but I can't find anything. I also tried looking online for answers but all the answers relate to earlier versions of XCode. Here's a pic: Error Message 回答1: Try