templates

C++ compile time check if method exists in template type

你说的曾经没有我的故事 提交于 2021-02-07 14:13:19
问题 I have a template that calls a member function. How do I check with static_assert that the method exists? struct A { }; struct B { int foo() { return 42; } }; template <typename T> struct D { static_assert(/* T has foo */, "T needs foo for reasons"); int bar() { return t.foo(); } T t; }; int main() { D<A> d; std::cout << d.bar() << std::endl; return 0; } I know this will just generate a compiler error that A does not have foo but I would like to check and give a better error output using

Call non constexpr from constexpr template function

与世无争的帅哥 提交于 2021-02-07 13:35:00
问题 I stumbled on constexpr template functions calling non constexpr functions: In the following snippet bar fails to compile as expected due to the call of non constexpr set but foo compiles. Can anyone tell me the reason why foo compiles? template<class T> void set(T& x){ x++; } template<class T> constexpr void foo(T& x){ set<T>(x); } constexpr void bar(int& x){ set<int>(x); } void bar(){ int x = 5; foo(x); bar(x); } The compiler fails to compile with the error: <source>: In function 'constexpr

Call non constexpr from constexpr template function

落花浮王杯 提交于 2021-02-07 13:33:27
问题 I stumbled on constexpr template functions calling non constexpr functions: In the following snippet bar fails to compile as expected due to the call of non constexpr set but foo compiles. Can anyone tell me the reason why foo compiles? template<class T> void set(T& x){ x++; } template<class T> constexpr void foo(T& x){ set<T>(x); } constexpr void bar(int& x){ set<int>(x); } void bar(){ int x = 5; foo(x); bar(x); } The compiler fails to compile with the error: <source>: In function 'constexpr

C++ Deduce template arguments based on other template arguments

岁酱吖の 提交于 2021-02-07 13:32:43
问题 Suppose I have the following class: template <class T, class U, class V> Foo { ... }; The template parameters have a distinct mapping, so I can deduce the other template arguments U and V based on what T is. For example, if T is double, U and V will always be some classes D1 and D2, and if T is float, U and V will always be some other classes F1 and F2. With that in mind, is there a way I can pass in only one template argument, and have the compiler deduce the other two parameters? I know the

C++ Deduce template arguments based on other template arguments

不打扰是莪最后的温柔 提交于 2021-02-07 13:32:33
问题 Suppose I have the following class: template <class T, class U, class V> Foo { ... }; The template parameters have a distinct mapping, so I can deduce the other template arguments U and V based on what T is. For example, if T is double, U and V will always be some classes D1 and D2, and if T is float, U and V will always be some other classes F1 and F2. With that in mind, is there a way I can pass in only one template argument, and have the compiler deduce the other two parameters? I know the

C++ Deduce template arguments based on other template arguments

旧街凉风 提交于 2021-02-07 13:31:43
问题 Suppose I have the following class: template <class T, class U, class V> Foo { ... }; The template parameters have a distinct mapping, so I can deduce the other template arguments U and V based on what T is. For example, if T is double, U and V will always be some classes D1 and D2, and if T is float, U and V will always be some other classes F1 and F2. With that in mind, is there a way I can pass in only one template argument, and have the compiler deduce the other two parameters? I know the

One template specialization for several enum values

为君一笑 提交于 2021-02-07 13:17:26
问题 Normally if I want to have a templated (data) class by enum I would write something like this enum class Modes : int { m1 = 1, m2 = 2, m3 = 3 }; template <Modes M> class DataHolder { }; template<> class DataHolder<Modes::m1> { public: int a = 4; }; Then if I want the same specialization for the Modes::m1 as for the Modes::m2 I would write the same specialization again. Is there a way to write one specialization for several enum values? I have tried it with SFINAE, but I am not succesfull.

Why does this code not compile in g++

浪子不回头ぞ 提交于 2021-02-07 13:11:27
问题 sample code given below is not compiled in g++. but it's working on visual studio. is it possible to use Template member function inside template class in g++ class Impl { public: template<class I> void Foo(I* i) { } }; template<class C> class D { public: C c; void Bar() { int t = 0; c.Foo<int>(&t); } }; int main() { D<Impl> d; d.Bar(); return 0; } 回答1: Because the statement in question depends on a template parameter, the compiler is not allowed to introspect C until instantiation. You must

using template specialization

一曲冷凌霜 提交于 2021-02-07 12:58:25
问题 Usual template structs can be specialized, e.g., template<typename T> struct X{}; template<> struct X<int>{}; C++11 gave us the new cool using syntax for expressing template typedefs: template<typename T> using YetAnotherVector = std::vector<T> Is there a way to define a template specialization for these using constructs similar to specializations for struct templates? I tried the following: template<> using YetAnotherVector<int> = AFancyIntVector; but it yielded a compile error. Is this

Name resolution of functions inside templates instantiated with qualified types

三世轮回 提交于 2021-02-07 12:52:24
问题 Consider the following C++ code example: namespace n { struct A {}; } struct B {}; void foo(int) {} template<typename T> void quux() { foo(T()); } void foo(n::A) {} void foo(B) {} int main() { quux<n::A>(); // Error (but works if you comment out the foo(int) declaration) quux<B>(); // Works return 0; } As indicated in the comment, the template instantiation quux<n::A>() causes a compiler error (on GCC 4.6.3): foo.cpp: In function ‘void quux() [with T = n::A]’: foo.cpp:22:16: instantiated from