template-specialization

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

Deduction of trailing template-argument in declaration of explicit specializations of function templates (no function arg. deduction)

对着背影说爱祢 提交于 2020-05-26 05:15:32
问题 (This question is a branch-out from the discussion in the comments of Template specialization of variable template and type deduction.) [temp.expl.spec]/10 states that [ emphasis mine]: A trailing template-argument can be left unspecified in the template-id naming an explicit function template specialization provided it can be deduced from the function argument type . [ Example: template<class T> class Array { /* ... */ }; template<class T> void sort(Array<T>& v); // explicit specialization

How to conditionally add a function to a class template?

对着背影说爱祢 提交于 2020-05-22 19:06:07
问题 I have a Matrix class template as follows: template<typename T, std::size_t nrows, std::size_t ncols> class Matrix { T data[nrows][ncols]; public: T& operator ()(std::size_t i, std::size_t j) { return data[i][j]; } }; What I want is to define a .setIdentity() function only for instantiations when nrows==ncols is true at compile time. And there will be no definition of .setIdentity() when nrows==ncols is false . What I am trying is using enable_if idiom, but that will define the function for

Dependencies when using C++20 conceptual template function specialization

独自空忆成欢 提交于 2020-05-14 05:53:27
问题 I was doing some tests with the following C++20 code (built with current GCC10): template <typename ToT, typename FromT> ToT myfunction(const FromT& pFrom) = delete; template <typename ToT, typename FromT> struct myclass { inline ToT myclassmethod(const FromT& pFrom) { return myfunction<ToT, FromT>(pFrom); } }; /* C++20 concepts code */ template <std::same_as<std::string> ToT, std::integral FromT> inline ToT myfunction(const FromT& pFrom) { return std::to_string(pFrom); } template <std::same

Dependencies when using C++20 conceptual template function specialization

十年热恋 提交于 2020-05-14 05:52:07
问题 I was doing some tests with the following C++20 code (built with current GCC10): template <typename ToT, typename FromT> ToT myfunction(const FromT& pFrom) = delete; template <typename ToT, typename FromT> struct myclass { inline ToT myclassmethod(const FromT& pFrom) { return myfunction<ToT, FromT>(pFrom); } }; /* C++20 concepts code */ template <std::same_as<std::string> ToT, std::integral FromT> inline ToT myfunction(const FromT& pFrom) { return std::to_string(pFrom); } template <std::same

Should I declare my function template specializations or is defining them enough?

孤者浪人 提交于 2020-05-09 07:34:08
问题 I have some classes which can be checked. The code which implements this declares a function template in a header file and specializes it in different source files: // check.h template <class T> bool check(const T& object); // class1.h struct Class1 {int mass;}; // check_class1.cpp #include "class1.h" #include "check.h" template <> bool check(const Class1& object) {return object.mass < 100;} // class2.h struct Class2 {int price;}; // check_class2.cpp #include "class2.h" #include "check.h"

Template specialization of variable template and type deduction

随声附和 提交于 2020-04-30 06:24:49
问题 template <class C> C fnc(); template <> int fnc(){return 0;} template <class C> C var; template <> int var = 0; // compile error int main() { } There's a specialization of a fnc function declared without an explicit type indication (such as int fnc<int>() ), so the type of template argument is deduced from the function return type, but that thing does not work for variable templates (it leads to compiler error). Is this a correct behavior or a bug in all compilers a have tested (clang, gcc)?

Template specialization of variable template and type deduction

风流意气都作罢 提交于 2020-04-30 06:24:12
问题 template <class C> C fnc(); template <> int fnc(){return 0;} template <class C> C var; template <> int var = 0; // compile error int main() { } There's a specialization of a fnc function declared without an explicit type indication (such as int fnc<int>() ), so the type of template argument is deduced from the function return type, but that thing does not work for variable templates (it leads to compiler error). Is this a correct behavior or a bug in all compilers a have tested (clang, gcc)?

Class template static data-member definition/declaration/initialization

核能气质少年 提交于 2020-03-24 00:50:26
问题 I know that the question has been asked several times and I've been reading posts like: Initializing static members of a templated class How can I Declare/define/initialize a static member variable of template classes as static member variables of a class? static member initialization for specialized template class However, I'm still struggling putting together all the pieces about templates, specializations, static data members definition and declarations. What I have is something like:

static_assert in function template with non-type template parameter

让人想犯罪 __ 提交于 2020-02-07 05:14:06
问题 I have a function template with integer template parameter. I would like to provide an implementation for particular integers only. An attempt to use the function template with another argument should cause a compilation error. I used static_assert in a way presented below. #include <type_traits> #include <iostream> template <typename T> struct false_type : public std::false_type {}; template <int T> void function() { static_assert(false_type<decltype(T)>::value, "Error"); }; template <> void