c++14

Why does order of declaring function changes overload chosen by SFINAE?

十年热恋 提交于 2019-12-05 20:03:32
This question is related to this answer . In this example SFINAE uses variable template has_literal_x specialization instead of the base template: struct A { }; A operator"" _x(char const*) { return {}; } template<typename T, typename S, typename = void> constexpr bool has_literal_x = false; template<typename T, typename S> constexpr bool has_literal_x<T, S, std::enable_if_t< std::is_same< decltype(operator""_x(std::declval<S>())), T >::value > > = true; int main() { std::cout << has_literal_x<A, char const*> << std::endl; // 1 } And here it uses the base template: template<typename T,

C++14: Initializing constexpr variables from parameter values

☆樱花仙子☆ 提交于 2019-12-05 19:30:28
问题 Say I have a class that that can return a constant expression through a constexpr function: template<int N> struct Foo { constexpr int Bar() const { return N; } }; If I wanted to initialize constexpr values from Foo::Bar() , how should I pass a parameter of type Foo ? I've tried these two, with an example constexpr variable inside of each to test that it can be initialized: template<int N> constexpr int ByValue(Foo<N> f) { constexpr int i = f.Bar(); return f.Bar(); } template<int N> constexpr

Build template from arguments of functions?

橙三吉。 提交于 2019-12-05 18:14:07
template<class... Foos> // N = sizeof...(Foos) template<typename... Args> // M = sizeof...(Args) void split_and_call(Args&&... args) { // Using Python notation here... Foos[0](*args[:a]); // a = arity of Foos[0] Foos[1](*args[a:b]); // b-a = arity of Foos[1] ... Foos[N-1](*args[z:M]); // M-z = arity of Foos[N-1] } Assumptions: All types in Foos are callable All types in Foos are unambiguous Any type in Foos may have an arity of 0 Args is the concatenation of all of the argument types used by Foos Can this be done with just Foos and without Args ? I'm actually not sure how to do it even if I

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

独自空忆成欢 提交于 2019-12-05 18:03:04
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 C++17) ? There is no such built-in method as far as I'm aware but you can implement it itself in a few

Explicit move constructor

核能气质少年 提交于 2019-12-05 17:22:38
问题 Trying to compile the following code: struct Foo { explicit Foo ( void ) { } explicit Foo ( Foo&& rhs ) { } }; Foo bar ( void ) { return Foo(); } Getting the following error: call to implicitly-deleted copy constructor of 'Foo' Well, it's quite obvious that the copy-ctor is implicitly deleted. Question 1: Why does the compiler need the copy-ctor of Foo ? I expected the return value of bar to be constructed from the rvalue Foo() with move-ctor. Then I redeclare the move-ctor as implicit and

Taylor series expansion as constexpr

こ雲淡風輕ζ 提交于 2019-12-05 15:49:55
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 trivial and also constexpr . I'm calling sine from within a loop like this: template <int N> void test

Is there a special rule for lambda in case of decltype(auto)?

怎甘沉沦 提交于 2019-12-05 15:42:12
问题 If I understood correctly this answer and referenced standard section [dcl.type.auto.deduct-5], the code: decltype(auto) a = e; is always equivalent to decltype( e ) a = e; But now the problem appears if instead of e I put the lambda expression to decltype(auto) : decltype(auto) lambda = [](){}; This compiles, to my surprise, successfully in both gcc and clang. Reason for the shock I've experienced lays in standard which says specifically that lambda should not occur in unevaluated operand

Why is it necessary to to use set.find(x) != set.end() while finding an element.

谁都会走 提交于 2019-12-05 15:10:50
I am wondering what is wrong when I use *(set.find(x)) == x instead of set.find(x)!=set.end() . It usually works but while attempting a question on Hackerrank (question : link ). This code gives CA for all test cases : int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ set<int>s; int n,x,y; cin >> n; while(n--){ cin >> y >> x; if(y==1) s.insert(x); else if(y==2) s.erase(x); else { set<int>::iterator it=s.find(x); if(it != s.end()) cout << "Yes" <<endl; else cout << "No" <<endl; } } return 0;} But this doesn't work for 2 test cases. The test case file is too

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

筅森魡賤 提交于 2019-12-05 13:58:47
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. 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 a static_assert suffices: [](auto &&arg) { static_assert(has_metod_f<std::decay_t<decltype(arg)>>::value,

pragma STDC FENV_ACCESS ON is not supported

谁说我不能喝 提交于 2019-12-05 13:46:41
I tried to slightly modify the example from the article : #include <iostream> #include <cfenv> #pragma STDC FENV_ACCESS ON int main() { std::feclearexcept(FE_ALL_EXCEPT); //int r = std::feraiseexcept(FE_UNDERFLOW | FE_DIVBYZERO); double x = 1.0; double y = 0.0; double result{}; asm volatile ("fldl %1\n" "fdivl %2\n" : "=%t"(result) : "m"(x), "m"(y) : "memory"); std::cout << result << std::endl; int e = std::fetestexcept(FE_ALL_EXCEPT); if (e & FE_DIVBYZERO) { std::cout << "division by zero\n"; } if (e & FE_INEXACT) { std::cout << "inexact\n"; } if (e & FE_INVALID) { std::cout << "invalid\n"; }