template-function

template friend functions of template class

情到浓时终转凉″ 提交于 2020-07-15 07:46:47
问题 I have the following template class and template function which intends to access the class' private data member: #include <iostream> template<class T> class MyVar { int x; }; template<class T> void printVar(const MyVar<T>& var) { std::cout << var.x << std::endl; } template<class T> void scanVar(MyVar<T>& var) { std::cin >> var.x; } struct Foo {}; int main(void) { MyVar<Foo> a; scanVar(a); printVar(a); return 0; } To declare the two functions as MyVar<T> 's friend functions, I've tried the

Inlining Template Specialization

烂漫一生 提交于 2020-05-27 02:18:33
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

Inlining Template Specialization

↘锁芯ラ 提交于 2020-05-27 02:16:00
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

Inlining Template Specialization

◇◆丶佛笑我妖孽 提交于 2020-05-27 02:14:58
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

Inlining Template Specialization

落爺英雄遲暮 提交于 2020-05-27 02:14:09
问题 If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template<typename T> void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a specalization to foo.h: template<> void foo(const bool param) { cout << param << endl; } Obviously I can solve this by inline 'ing the specialization. My question is, why do I need to? If the template doesn't violate ODR, why does a specialization? 回答1: An

How to write a template function that takes an array and an int specifying array size

喜夏-厌秋 提交于 2020-05-15 02:33:10
问题 For a university exercise, I have been asked to write a template function "print();", which takes two arguments, 1: an array of a generic type, and 2: an int specifying the size of the array. The function should then print out every item in the array to the console. I am having some trouble with the function arguments. The code I currently have is: template <typename Type> Type print (Type a, Type b) { Type items; Type array; a = array; b = items; for (int i = 0; i < items; i++) { std::cout <

SFINAE : Delete a function with the same prototype

白昼怎懂夜的黑 提交于 2020-05-07 19:00:25
问题 I wonder what is the difference between this code that works : #include <type_traits> #include <iostream> template<typename T> using is_ref = std::enable_if_t<std::is_reference_v<T>, bool>; template<typename T> using is_not_ref = std::enable_if_t<!std::is_reference_v<T>, bool>; template<typename T, is_ref<T> = true> void foo(T&&) { std::cout << "ref" << std::endl; } template<typename T, is_not_ref<T> = true> void foo(T&&) { std::cout << "not ref" << std::endl; } int main() { int a = 0; foo(a)

adapting a non-constexpr integral value to a non-type template parameter, and code bloat

一笑奈何 提交于 2020-01-21 07:30:29
问题 Consider a function object F taking a constexpr size_t argument I struct F { template <size_t I> constexpr size_t operator()(size <I>) const { return I; } }; wrapped within a type size <I> , where (for brevity) template <size_t N> using size = std::integral_constant <size_t, N>; Of course, we could pass I directly but I want to emphasize that it is constexpr by using it as a template argument. Function F is dummy here but in reality it could do a variety of useful stuff like retrieving

passing member-function as argument to function-template

余生长醉 提交于 2020-01-14 13:29:10
问题 Consider three ways to implement a routine in c++: through functors, member functions, and non-member functions. For example, #include <iostream> #include <string> using std::cout; using std::endl; using std::string; class FOO { public: void operator() (string word) // first: functor { cout << word << endl; } void m_function(string word) // second: member-function { cout << word << endl; } } FUNCTOR; void function(string word) // third: non-member function { cout << word << endl; } Now

Template function calls to other functions

╄→гoц情女王★ 提交于 2020-01-06 14:49:05
问题 I understand the template functions usually are to be declared and defined in header files. The problem I am having is that my template function makes calls to other functions. The prototypes of those other functions are in the same header file before the template function itself. That portion of the code: //header.h template <int ignoreAdetection> __global__ void MCMLKernel(SimState d_state, GPUThreadStates tstates) { // photon structure stored in registers PhotonStructGPU photon; // random