template-specialization

Partial specialisation of member function with non-type parameter

北城余情 提交于 2019-12-04 03:20:18
I have a template class with both a type and a non-type template parameter. I want to specialize a member function, what I finding is, as in the example below, I can do a full specialization fine. template<typename T, int R> struct foo { foo(const T& v) : value_(v) {} void bar() { std::cout << "Generic" << std::endl; for (int i = 0; i < R; ++i) std::cout << value_ << std::endl; } T value_; }; template<> void foo<float, 3>::bar() { std::cout << "Float" << std::endl; for (int i = 0; i < 3; ++i) std::cout << value_ << std::endl; } However this partial specialization won't compile. template<int R>

c++ template specialization - linker error multiple definitions

十年热恋 提交于 2019-12-04 00:48:16
My third question here today ;-), but I am really new to c++ template programming and operator overloading. I am trying the following: terminallog.hh //snipped code class Terminallog { public: Terminallog(); Terminallog(int); virtual ~Terminallog(); template <class T> Terminallog & operator<<(const T &v); template <class T> Terminallog & operator<<(const std::vector<T> &v); template <class T> Terminallog & operator<<(const T v[]); Terminallog & operator<<(const char v[]); //snipped code }; //snipped code template <class T> Terminallog &Terminallog::operator<<(const T &v) { std::cout << std:

Define template specialization in cpp?

放肆的年华 提交于 2019-12-04 00:30:14
I can define a specialized function in a cpp like so... // header template<typename T> void func(T){} template<> void func<int>(int); // cpp template<> void func<int>(int) {} How can I define a method in a specialized class in a cpp? Like so (which doesn't work, I get error C2910: 'A<int>::func' : cannot be explicitly specialized )... // header template<typename T> struct A { static void func(T){} }; template<> struct A<int> { static void func(int); }; // cpp template<> void A<int>::func(int) {} Use following syntax in your .cpp file: void A<int>::func(int) { } This is Visual C++ kinda feature

Design pattern to avoid downcasting in message passing

你。 提交于 2019-12-03 15:19:34
问题 Base class MessageHandler has derived classes. They would like to pass messages to each other. Messages could be of different classes, but can be made to share a base class. How can each MessageHandler avoid downcasting a received message? Is it somehow possible to do something that has the effect of template-parametrizing the virtual receiveMessage function on MessageHandler? Essentially, I'm trying to replace the following code with something that does not downcast, and is hopefully a

c++ function template specialization for known size typedefed array

和自甴很熟 提交于 2019-12-03 13:58:38
问题 Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void func( Type var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; std::cout << "-> var is SCALAR. Size = " << sizeof( Type ) << std::endl; } #if 1 template< typename Type > void func( Type * var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; std::cout << "-> var is ARRAY.

What is the best way to create a specialization-only function template?

ε祈祈猫儿з 提交于 2019-12-03 13:14:45
Is there a better way to do the following? #include <iostream> template <typename T> T Bar(); template <> int Bar<int>() { return 3; } // Potentially other specialisations int main() { std::cout << Bar<int>() << std::endl; // This should work std::cout << Bar<float>() << std::endl; // This should fail } The problem with this solution is that it fails at (understandably) link time with "undefined reference to float Bar<float>() " or the like. This can be confusing for other developers as they may suspect an implementation file is not being linked. I do know another potential solution: template

Ambiguous partial specializations with Clang in C++17

浪子不回头ぞ 提交于 2019-12-03 11:19:15
template <typename Foo, Foo Part> struct TSelect {}; enum What { The }; template <typename Foo> struct AnotherOneSelector { static constexpr Foo Id = Foo::The; }; template <typename Foo, typename SelectPartType> struct THelper; template <typename Foo> struct THelper<Foo, TSelect<Foo, AnotherOneSelector<Foo>::Id>> {}; template <typename Foo, Foo PartId> struct THelper<Foo, TSelect<Foo, PartId>> {}; int main() { THelper<What, TSelect<What, What::The>> t; } This code compiles with gcc8.1 with each of the standard option (c++11, c++14, c++17), but clang trunk does not with c++17 (although with c+

function template specialization compile error

不问归期 提交于 2019-12-03 10:52:06
##A.hh template<class T> void func(T t) {} template<> void func<int>(int t) {} void func2(); ##A.cpp void func2() {} ##main.cpp func("hello"); func(int()); The error I get is: error LNK2005: "void __cdecl func(int)" (??$func@H@@YAXH@Z) already defined in A.obj, one or more multiply defined symbols found Is a function template specialization not treated as a normal function template? It looks like it will be in the objective file for A. As template<> void func<int>(int t) {} is a function overload rather than a function template (i.e., all types are known at the point of definition so it is no

How to specialize Iterator by its value type, in C++?

China☆狼群 提交于 2019-12-03 10:37:41
Is it possible to specialize an Iterator template parameter by its value_type ? I have a function with the following prototype. template<typename InputIterator> void f(InputIterator first, InputIterator last); And I want to handle specially if InputIterator::value_type is SomeSpecificType. Michael Anderson You can use some intermediate structs to get the partial template specialisation that you need. Something like this should do the trick template<typename T, typename V> struct f_impl { static void f( T first, T last ) {...}; //Default version }; template<typename T> struct f_impl<T,

Is it possible to implement always_false in the C++ standard library?

守給你的承諾、 提交于 2019-12-03 10:28:59
There are cases where one uses an always_false helper to e.g. cause unconditional static_assert failure if instantiation of some template is attempted: template <class... T> struct always_false : std::false_type {}; template<class T> struct UsingThisShouldBeAnError { static_assert(always_false<T>::value, "You should not use this!"); }; This helper is necessary because a template definition must (at least theoretically) have at least one set of template parameters for which a valid specialization can be produced in order for the program to be well-formed: [temp.res]/8 : The program is ill