template-specialization

total class specialization for a template

二次信任 提交于 2020-01-04 02:57:08
问题 lets say i have a templated class template <typename T> struct Widget { //generalized implementation } but i wanted to totally specialize.. for a template that accepted a parameter? template <> struct Widget< TemplateThatAcceptsParameter<N> > { //implementation for Widget for TemplateThatAcceptsParameterN //which takes parameter N } How does one go about doing this? 回答1: This is called a partial specialization and can be coded like this: template <typename T> struct Widget { //generalized

Partial Template Specialization restricted to certain types

百般思念 提交于 2020-01-02 04:05:27
问题 is it possible to write a partial template specialization that is used only for class types that, for example, inherit from a specific class or comply with some other constraint that can be expressed via type traits? i.e., something like this: class A{} class B : public A{} template<typename T> class X{ int foo(){ return 4; } }; //Insert some magic that allows this partial specialization //only for classes which are a subtype of A template<typename T> class X<T>{ int foo(){ return 5; } }; int

Getting “illegal use of explicit template arguments” when doing a pointer partial specialization for a class method

一世执手 提交于 2020-01-02 00:55:09
问题 Hello I'm having problems with partial specialization. What I want to do is have a class that has a template member function that will interpret a given value to one specified by the user. For instance the class name is Value and here is a snippet of what I want to do: int *ptr1 = new int; *ptr1 = 10; Value val1 = ptr1; int *ptr2 = val1.getValue<int*>(); Value val2 = 1; int testVal = val2.getValue<int>(); Here is how I implemented such class: struct Value { Value(void *p) : val1(p){} Value

Partial specialisation of member function with non-type parameter

泄露秘密 提交于 2020-01-01 08:43:21
问题 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)

Using template parameters as template parameters

一世执手 提交于 2019-12-31 03:38:06
问题 Why is the following code invalid? template <typename S, typename T> struct B{ void f(T t, S s) {t.f<S>(s); } }; gcc 4.3.4 complains that it "expected primary-expression before '>' token", i.e. that "S" wasn't a valid primary-expression. 回答1: You need to specify that f is a template: void f(T t, S s) { t.template f<S>(s); } C++ doesn’t know this (at this point) since f ’s type depends on the type of the template parameter T . Furthermore, the following syntax would be ambiguous: does < mean

hide function template, declare specializations

冷暖自知 提交于 2019-12-31 03:31:28
问题 This is a followup to C++ templates: prevent instantiation of base template I use templates to achieve function overloading without the mess of implicit type conversions: declare the function template, define desired specializations (overloads). all is well except wrong code does not produce errors until the link phase: lib.hpp: template<class T> T f(T v); lib.cpp: #include "lib.hpp" template<> long f(long v) { return -v; } template<> bool f(bool v) { return !v; } main.cpp: #include <iostream

decltype for overloaded member function [duplicate]

点点圈 提交于 2019-12-30 08:12:27
问题 This question already has an answer here : Disambiguate overloaded member function pointer being passed as template parameter (1 answer) Closed 3 years ago . I have this code: struct Foo { int print(int a, double b); int print(int a); void print(); void print(int a, int b, int c); void other(); }; I can call decltype(&Foo::other) but calling decltype(&Foo::print) end with error, which is clear to me. But how can I specify more "closely" which of the four print methods, I want to resolve to

Is it legit to specialize variadic template class inside other template class

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 07:26:02
问题 Consider a code: #include <iostream> template <class T> struct outer { template <class... Args> struct inner { static constexpr bool value = false; }; template <class... Other> struct inner<T, Other...> { static constexpr bool value = true; }; }; int main() { std::cout << outer<int>::inner<int, void>::value << std::endl; }; It does compile in both g++ and clang++ but I am not convinced it is legal. As far as I know one cannot for example specialize template method for template class if not

function template specialization generating link error [duplicate]

不羁岁月 提交于 2019-12-25 01:38:36
问题 This question already has answers here : multiple definition of template specialization when using different objects (4 answers) Explicit specialization of function templates causes linker error (2 answers) Closed 6 months ago . I had previously asked this question that involved using auto with variadic templates that generates a tuple and the proper way to iterate over them. User metalfox had provided me with this solution. I tried their solution and this is what my full code looks like

C++ - specialising member function template via templated functor does not compile

℡╲_俬逩灬. 提交于 2019-12-24 14:11:12
问题 I wish to create a class that can convert between arrays of floats and doubles polymorphically. That is, the instance concerned (parameterised by <double> or <float> ) and the decision to pass a float* or double* is decided at runtime, not statically. As a proposed answer to another question, but modified according to this answer (because I understand it's not possible to fully specialise a member function template inside a class), a pure virtual base class BaseDest that provides simple