template-specialization

Why do templates specialisations need to be inlined?

守給你的承諾、 提交于 2020-01-21 01:34:01
问题 I am referring to this answer: https://stackoverflow.com/a/4447057/930315 I ran into a similar issue as the OP of the cited question, having a function template<typename T> void func(T& val); and its specialization template<> void func<mytype>(mytype& val); resulted in a duplicate symbols linker error (the methods are implemented in a '.tpp' file that is included at the end of my header). adding inline to the specialised function resolved the issue. Why? 回答1: According to clause 3.2:4 in the

Function Template Overloading or Specialization for inner template type std::vector<std::vector<T>>

纵然是瞬间 提交于 2020-01-14 14:01:29
问题 How to achieve function Template Overloading for inner template type std::vector<std::vector<T>> . I have a program of overloaded templates and a complex data structure having maps, pairs and vectors. #include <iostream> #include <vector> #include <map> #include <utility> #include <typeinfo> template<typename Test, template<typename...> class Ref> //#6 struct is_specialization : std::false_type {}; template<template<typename...> class Ref, typename... Args> //#7 struct is_specialization<Ref

Best way (Or workaround) to specialize a template alias

爷,独闯天下 提交于 2020-01-14 13:32:12
问题 Im currently implementing a tiny metaprogramming-based compile-time computations library. If have defined a base class for operators, wich has a result typedef (I have decided to use integral wrappers like std::integral_constant as values instead of raw integral values, to provide an uniform interface along the library), and a n-ary operator base class, that checks if the operators has at least one operand: template<typename RESULT> struct operator { using result = RESULT; }; template

Best way (Or workaround) to specialize a template alias

只愿长相守 提交于 2020-01-14 13:32:06
问题 Im currently implementing a tiny metaprogramming-based compile-time computations library. If have defined a base class for operators, wich has a result typedef (I have decided to use integral wrappers like std::integral_constant as values instead of raw integral values, to provide an uniform interface along the library), and a n-ary operator base class, that checks if the operators has at least one operand: template<typename RESULT> struct operator { using result = RESULT; }; template

Why are there so many specializations of std::swap?

旧巷老猫 提交于 2020-01-13 07:36:07
问题 While looking at the documentation for std::swap, I see a lot of specializations. It looks like every STL container, as well as many other std facilities have a specialized swap. I thought with the aid of templates, we wouldn't need all of these specializations? For example, If I write my own pair it works correctly with the templated version: template<class T1,class T2> struct my_pair{ T1 t1; T2 t2; }; int main() { my_pair<int,char> x{1,'a'}; my_pair<int,char> y{2,'b'}; std::swap(x,y); } So

Specializing std::hash to derived classes

你离开我真会死。 提交于 2020-01-09 11:40:13
问题 I have an abstract base class Hashable that classes that can be hashed derive from. I would now like to extend std::hash to all classes that derive from Hashable . The following code is supposed to do exactly that. #include <functional> #include <type_traits> #include <iostream> class Hashable { public: virtual ~Hashable() {} virtual std::size_t Hash() const =0; }; class Derived : public Hashable { public: std::size_t Hash() const { return 0; } }; // Specialization of std::hash to operate on

How to symmetrically implement serialize and deserialize template functions in C++

冷暖自知 提交于 2020-01-07 04:20:22
问题 I want to write a serial of template functions to serialize and deserialize objects. I've finished the serialization part and everything works: #ifndef SERIALIZE_H #define SERIALIZE_H #include <string> #include <vector> #include <unordered_set> #include <unordered_map> #include <memory> inline std::string to_json(int value) { return std::to_string(value); } inline std::string to_json(long value) { return std::to_string(value); } inline std::string to_json(double value) { return std::to_string

Function template parameters failing to convert type during compilation

瘦欲@ 提交于 2020-01-06 11:20:14
问题 While trying to use a function template that calls a class's specialized static function template it is failing to convert its parameter from the template parameter list. Here is the function that I'm calling in main: template<class Engine, typename Type, template<typename = Type> class Distribution, class... DistParams> Type randomGenerator( RE::SeedType seedType, std::size_t seedValue, std::initializer_list<std::size_t> list, DistParams... params ) { static Type retVal = 0; static Engine

Class template: why can't I specialize a single method for void type?

六眼飞鱼酱① 提交于 2020-01-05 08:56:59
问题 I have a class template: template <typename Argument> class Signal { void invoke(Argument arg) {} }; Now I want to invoke signals without parameter (meaning void parameter). I assume I could specialize the whole class for void and it would compile. But there's a lot of code in the class, I don't want to duplicate it. I want to only specialize whatever neccessary. So I try adding: // void specialization template<> void Signal<void>::invoke() { } And get an error: error C2244: 'Signal::invoke'

choosing appropriate specialized template at runtime

☆樱花仙子☆ 提交于 2020-01-04 14:15:11
问题 I am using a class from a 3rd party library which look like, template <typename A = DefaultT, typename B = DefaultT, typename C = DefaultT, typename D = DefaultT, typename E = DefaultT, typename F = DefaultT> class Vertex {}; I want to use a partial specialization of this class at runtime depending on conditions, for example, class MyA {}; class MyB {}; class MyC {}; class MyD {}; bool useA, useB, useC, useD; //these booleans change at runtime // I want to specialize Vertex depending on the