templates

Typedef with template parameter in C++ [duplicate]

倖福魔咒の 提交于 2021-02-07 08:58:30
问题 This question already has an answer here : C++ template typedef (1 answer) Closed 6 years ago . How can I solve this error? My header file template<typename T> class C1 { public: typedef std::vector<T::F> TFV; TFV Function1(); }; My CPP file template<typename T> TFV C1::Function() //error: ‘TFV’ does not name a type { } 回答1: First of all, use the typename keyword to tell the compiler to interpret F as the (qualified) name of a type: typedef std::vector<typename T::F> TFV; // ^^^^^^^^ Secondly

Template Specialization Not Working with Derived Class

人走茶凉 提交于 2021-02-07 08:45:28
问题 I'm doing template specialization for one of my classes, and I'm experiencing something unexpected.. This is my code: class Base {}; class Derived : public Base {}; template<typename T> void doSomething(T t) { cout << "All Types!" << endl; } template<> void doSomething(Base b) { cout << "Base!" << endl; } int main() { Derived d; doSomething(d); //prints "All Types!" return 0; } I have a template function doSomething(T) that accepts parameters of any type.. except for type Base class. So I

Template Specialization Not Working with Derived Class

微笑、不失礼 提交于 2021-02-07 08:45:16
问题 I'm doing template specialization for one of my classes, and I'm experiencing something unexpected.. This is my code: class Base {}; class Derived : public Base {}; template<typename T> void doSomething(T t) { cout << "All Types!" << endl; } template<> void doSomething(Base b) { cout << "Base!" << endl; } int main() { Derived d; doSomething(d); //prints "All Types!" return 0; } I have a template function doSomething(T) that accepts parameters of any type.. except for type Base class. So I

Template Specialization Not Working with Derived Class

半世苍凉 提交于 2021-02-07 08:44:36
问题 I'm doing template specialization for one of my classes, and I'm experiencing something unexpected.. This is my code: class Base {}; class Derived : public Base {}; template<typename T> void doSomething(T t) { cout << "All Types!" << endl; } template<> void doSomething(Base b) { cout << "Base!" << endl; } int main() { Derived d; doSomething(d); //prints "All Types!" return 0; } I have a template function doSomething(T) that accepts parameters of any type.. except for type Base class. So I

Overloading of hidden friends by differences only in (mutually exclusive) requires-clauses: legal or an ODR-violation?

有些话、适合烂在心里 提交于 2021-02-07 06:13:31
问题 Consider the following class template, which contains two (hidden) friend declarations of the same friend (same function type ; see below), which also defines the friend (and the friend is thus inline), but with the definition conditional on (mutually exclusive) requires-clauses : #include <iostream> struct Base {}; template<int N> struct S : public Base { friend int foo(Base&) requires (N == 1) { return 1; } friend int foo(Base&) requires (N == 2) { return 3; } }; [dcl.fct]/8 states that

template object's template friend functions and namespaces

一世执手 提交于 2021-02-07 06:13:05
问题 In the following C++ example code, GCC 6 and Clang 3.8 disagree on what the correct behaviour is: This contrived example "works" -- as in the test() function returns o.p in GCC. In clang, it calls the (undefined) function get<int, int, float, double> : template<typename ...Args> class obj { bool p = false; template<typename T, typename... Args2> friend T get(const obj<Args2...> &o) { return o.p; } }; template<typename T, typename... Args> T get(const obj<Args...> &o); bool test(const obj<int,

template object's template friend functions and namespaces

家住魔仙堡 提交于 2021-02-07 06:09:57
问题 In the following C++ example code, GCC 6 and Clang 3.8 disagree on what the correct behaviour is: This contrived example "works" -- as in the test() function returns o.p in GCC. In clang, it calls the (undefined) function get<int, int, float, double> : template<typename ...Args> class obj { bool p = false; template<typename T, typename... Args2> friend T get(const obj<Args2...> &o) { return o.p; } }; template<typename T, typename... Args> T get(const obj<Args...> &o); bool test(const obj<int,

Can we use variadic template function to filter parameters of specific type, then pass the rest to another function?

两盒软妹~` 提交于 2021-02-07 05:57:06
问题 for example // we have a variadic function void print(...); // I need such a function to filter parameters of specific type template<typename... Args> void print_filter(const Args&... args) { // filter non-integral type print(integral args); } // my goal, all non-integral type can be ignored: print_filter(1.0, 2, "abc", 3) == print(2, 3) I have used up my knowledge to do that... can you help? or just to prove it's impossible, which also be very helpful. Thanks 回答1: A neat trick is to convert

N-dimensionally nested metaloops with templates

丶灬走出姿态 提交于 2021-02-07 05:51:07
问题 I am trying to do N-dimensionally nested metaloops with template metaprogramming. The nesting part is trivial, however passing all the arbitrary number of iteration indices as template parameters to the most-inner loop seems problematic. A simple unnested metaloop looks like: template <size_t I, size_t N> struct meta_for { template <typename Lambda> inline meta_for(Lambda &&iteration) { iteration(I); meta_for<I+1, N> next(static_cast<Lambda&&>(iteration)); } }; template <size_t N> struct meta

Checking if variadic template parameters are unique using fold expressions

为君一笑 提交于 2021-02-07 05:49:05
问题 Given a variadic template parameter pack, I want to check if all types given to it are unique using an inline constexpr bool and fold expressions. I trie something like this: template<class... T> inline static constexpr bool is_unique = (... && (!is_one_of<T, ...>)); Where is_one_of is a similar bool that works correctly. But this line doesn't compile regardless of what I put into is_one_of. Can this even be done using fold expressions, or do I need to use a regular struct for this purpose?