Questions tagged [explicit-specialization]

71 questions
62
votes
1 answer

What can and can't I specialize in the std namespace?

Users are allowed to add explicit specializations to the std namespace. However, there are a few templates that I am explicitly forbidden from specializing. What templates can and can't I specialize?
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
18
votes
2 answers

Is there provision specifying the restriction on "return type" in a function template explicit specialization?

template void fun(T){} template<> int fun(int){return 0;} Consider this example, it is rejected by all implementations. However, I haven't found any persuasive provision in the current standard that specifies this explicit specialization…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
16
votes
1 answer

How to properly use GHC's SPECIALIZE pragma? (Example: specializing pure function from monadic ones using Identity.)

As an example, suppose I want to write a monadic and non-monadic map over lists. I'll start with the monadic one: import Control.Monad import Control.Monad.Identity mapM' :: (Monad m) => (a -> m b) -> ([a] -> m [b]) mapM' _ [] = return [] mapM' f…
Petr
  • 62,528
  • 13
  • 153
  • 317
14
votes
5 answers

Difference between explicit specialization and regular functions when overloading a template function

I'm on a roll today. Here goes n00b question number 7: What's the difference between explicit specialization and just regular functions when you try to overload a template function? What's the appropriate situation to use the explicit…
Pwnna
  • 9,178
  • 21
  • 65
  • 91
12
votes
1 answer

Explicit specialization of member function template in source file

I have a class with a member template function: // writer.h class Writer { public: ... template void addField(const std::string& name, V v) { // write something } }; And in Writer's source file,…
Barry
  • 286,269
  • 29
  • 621
  • 977
12
votes
1 answer

class template state data member, not an entity that can be explicitly specialized

I got an error in the code below: template::value> class class_name; template<> class class_name{ public: static string const value; }; template<> string const class_name
12
votes
2 answers

Array type deduction in a function template

I have a template method as follows:- template void ProcessArray(T array[length]) { ... } And then I have code using the above method:- int numbers[10] = { ... }; ProcessArray(numbers); My question is why do I have…
Higher-Kinded Type
  • 1,964
  • 5
  • 27
  • 44
11
votes
2 answers

Can a class template explicit specialization also declare something else?

It would be nice if this code were invalid. But it's conceptually sound, and GCC accepts it although Comeau doesn't: template< typename > struct t; template<> struct t< int > {} r; // Bad declarator! Don't pee on the carpet! (Edit: the above…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
10
votes
1 answer

Template static definition and explicit specialization instantiation errors in MSVC

I'm wondering why the following code runs just fine in gcc #include using namespace std; template struct F { static T const value; }; template<> struct F { // Specialization static int const value; };…
9
votes
1 answer

Nested class explicit specilization: different compiler behavior

The following code compiles fine with clang++ 6.0.0 and g++ 7.3.0 (compilation flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors) but fails to compile with vc++ 19.10.25017 (compilation flag is /Za): template struct A { …
9
votes
6 answers

Selecting an explicit specialization of a class based on a derived type

Hi I'm having problems selecting the correct version of a templated class which has an explicit specialization. I'm wanting to select a specialization using a derived class of the class used to specialize. The scenario is: #include…
user176168
  • 1,294
  • 1
  • 20
  • 30
8
votes
1 answer

An explicit specialization cannot be a friend declaration

The code template void foo(const T& t) {} template class A { template <> friend void foo(const T& t) {} }; gives compile error "defining explicit specialization ‘foo’ in friend declaration friend void…
8
votes
1 answer

SFINAE: detect existence of a template function that requires explicit specialization

As a follow-up to my previous question, I am trying to detect the existence of a template function that requires explicit specialization. My current working code detects non-template functions (thanks to DyP's help), provided they take at least one…
syam
  • 14,701
  • 3
  • 41
  • 65
7
votes
3 answers

Why is this C++ explicit template specialization code illegal?

(Note: I know how it is illegal, I'm looking for the reason that the language make it so.) template void Foo(); // Note: no generic version, here or anywhere. int main(){ Foo(); return 0; } template<> void…
BCS
  • 75,627
  • 68
  • 187
  • 294
7
votes
2 answers

explicit specialization of class method - symbol already defined

The One Definition Rule states that a program should contain one definition of every non-inline function. For members of template classes, this not entirely clear to me: /////////// // Tfoo.h template class A { void foo(){} …
xtofl
  • 40,723
  • 12
  • 105
  • 192
1
2 3 4 5