template-classes

Disable a function by throwing error at compile-time with template class using traits

谁说我不能喝 提交于 2019-12-07 14:06:35
问题 I have a class, let's call it Foo with several methods: template<typename T> class Foo { public: Foo() { /* ... */ } bool do_something() { /* ... */ } // This method should be callable only if: // std::is_floating_point<T>::value == true void bar() { // Do stuff that is impossible with integer } }; I would like to be able to construct both Foo<double> and Foo<int> But I don't want to allow calls to bar() when the type T is not a floating point type. I also want the error to be generated at

Dynamic Allocation in Template Class Constructor

谁说我不能喝 提交于 2019-12-06 15:46:33
I am working on a stack class and have two constructors. One of interest is this one. template <typename T> stack<T>::stack( const int n) { capacity = n ; size = 0 ; arr = new T [capacity] ; } I am calling it inside main like this. stack<int> s1(3) ; The program compiles fine but i get this run time error. 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall stack<int>::~stack<int>(void)" (??1?$stack@H@@QAE@XZ) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall stack<int>::stack<int>(int)" (??0?$stack@H@@QAE@H@Z)

Pull Apart Function Type With Specialized Function

允我心安 提交于 2019-12-01 10:42:41
The answer to this question picks apart a function type using a class template: template <typename T> struct function_args {}; template <typename R, typename... Args> struct function_args<R(Args...)> { using type = tuple<Args...>; }; template <typename T> using decltypeargs = typename function_args<T>::type; As I studied what was being done here I tried to rewrite function_args . I attempted to do this using a function so as to eliminate the need for the decltypeargs template. But found myself mired in improper syntax: template <typename T> tuple<> myTry(); template <typename Ret, typename...

Pull Apart Function Type With Specialized Function

假装没事ソ 提交于 2019-12-01 08:51:57
问题 The answer to this question picks apart a function type using a class template: template <typename T> struct function_args {}; template <typename R, typename... Args> struct function_args<R(Args...)> { using type = tuple<Args...>; }; template <typename T> using decltypeargs = typename function_args<T>::type; As I studied what was being done here I tried to rewrite function_args . I attempted to do this using a function so as to eliminate the need for the decltypeargs template. But found

function as template parameter : if(T receive 2 param)T(a,b); else T(a);

徘徊边缘 提交于 2019-12-01 08:25:53
How to make template class Collection<K,T> receive a function T - that can either has signature T(K) or T(K,int) - as template argument, then conditionally compile base on the signature of the function? Here is the existing code that can receive 1 signature : Collection<K,HashFunction(K)> . template<typename AA> using HashFunction= HashStruct& (*)(AA ); /** This class is currently used in so many places in codebase. */ template<class K,HashFunction<K> T> class Collection{ void testCase(){ K k=K(); HashStruct& hh= T(k); /*Collection1*/ //.... something complex ... } }; I want it to also support

C++ class template undefined reference to function [duplicate]

白昼怎懂夜的黑 提交于 2019-12-01 04:18:02
This question already has an answer here: undefined reference to template function [duplicate] 2 answers I keep getting undefined reference when i call the two functions from my template class "add" and "greater" in my main function. So, i have: number.h #ifndef NUMBER_H #define NUMBER_H template <class T> class number { public: T x; T y; number (int a, int b){ x=a; y=b;} int add (T&); T greater (); }; #endif number.cpp #include "number.h" template <class T> int number<T>::add (T& rezAdd){ rezAdd = x+y; return 1; } template <class T> T number<T>::greater (){ return x>y? x : y; } And my main

C++ class template undefined reference to function [duplicate]

孤街醉人 提交于 2019-12-01 01:52:46
问题 This question already has answers here : undefined reference to template function [duplicate] (2 answers) Closed 2 years ago . I keep getting undefined reference when i call the two functions from my template class "add" and "greater" in my main function. So, i have: number.h #ifndef NUMBER_H #define NUMBER_H template <class T> class number { public: T x; T y; number (int a, int b){ x=a; y=b;} int add (T&); T greater (); }; #endif number.cpp #include "number.h" template <class T> int number<T