Questions tagged [explicit-specialization]

71 questions
6
votes
6 answers

How should I do this explicit specialization?

Is following design possible?: template class Test{ public: template void doSomething(); //rest of things private: T obj; //some things }; Now if it was possible I'd do some explicit specializations for…
Pooria
  • 2,017
  • 1
  • 19
  • 37
6
votes
1 answer

Why won't "extern template" work with shared_ptr?

I had the (seemingly) bright idea of using extern template class std::shared_ptr in stdafx.h immediately after #include in order to prevent std::shared_ptr from being redundantly instantiated in…
dlf
  • 9,045
  • 4
  • 32
  • 58
5
votes
2 answers

std::unordered_set as member of class Foo

I'm writing a class that has an unordered_set of its own type as a member. Therefore I need to write a specialization for hash. This specialization needs to be defined after Foo is declared. But it seems to me as if I already need the…
devmapal
  • 53
  • 3
5
votes
1 answer

Explicit specialization of template variable

Similar to this question about explicit specialisation of static const class members of a template class, and this question about explicit specialisation of a template class, but my issue is with explicit specialisation of a variable template. My…
5
votes
1 answer

C++ function template specialization and overloading

Considering this code: template void f(T p) { //(1) cout << "Second" << endl; } template <> void f(int *p) { //(2) cout << "Third" << endl; } template void f(T* p) { //(3) cout << "First"…
user42768
  • 1,951
  • 11
  • 22
5
votes
1 answer

Is it valid to do explicit template specialisation with auto return 'type' in C++14?

Previous question. I repeat the code from the previous question to make this question self-contained. The code below compiles and does not issue any warnings if it is compiled using gcc 4.8.3. with -std=c++1y. However, it does issue warnings if…
user1391279
5
votes
2 answers

How to provide a explicit specialization to only one method in a C++ template class?

I have a template class that looks something like this: template class C { void A(); void B(); // Other stuff }; template void C::A() { /* something */ } template void C::B() { /* something */ } What I…
BCS
  • 75,627
  • 68
  • 187
  • 294
4
votes
1 answer

explicit specialization of 'CheckIntMap<>' after instantiation

template constexpr int CheckIntMap(const char *szStr, int nDefaultInt, const char *szOptStr1, int nOptInt1, U&&... u) { return (CheckIntMap(szStr, nDefaultInt, szOptStr1, nOptInt1) == nOptInt1) ? nOptInt1 : CheckIntMap(szStr,…
4
votes
2 answers

explicit member specialization

g++ 3.4.5 accepts this code: template struct A { static const char* const str; }; struct B {}; typedef A C; template<> const char* const C::str = "B"; // Equivalent to following? // template<> const char* const A::str =…
aschepler
  • 70,891
  • 9
  • 107
  • 161
4
votes
3 answers

How to do one explicit specialization for multiple types?

Considering a template function like below how is it possible to do explicitly specialize one version of function for multiple types: template void doSomething(){ //whatever } The intention is to have one specialization instead of…
Pooria
  • 2,017
  • 1
  • 19
  • 37
4
votes
1 answer

Why do I get missing symbols for an explicit template specialization in a static library?

If I compile the following code: // // g++ static.cpp -o static.o // ar rcs libstatic.a static.o // #include template < typename T > struct TemplatedClass { void Test( T value ) { std::cout << "Foobar was: " << value <<…
jkp
  • 78,960
  • 28
  • 103
  • 104
4
votes
1 answer

Explicitly specializing a deleted primary template

GCC accepts this but Clang rejects it as a redefinition: template< typename > void s() = delete; template<> void s< int >() {} Who is right?
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
3
votes
6 answers

Template class specialized inside and outside lib

Consider this synthetic example. I have two native C++ projects in my Visual Studio 2010 solution. One is console exe and another is lib. There are two files in lib: // TImage.h template class TImage { public: TImage() { #ifndef _LIB …
Mikhail
  • 20,685
  • 7
  • 70
  • 146
3
votes
0 answers

Explicit specialization in class: difference between Clang and GCC

The code below compiles and works correctly in Clang but in GCC it gives an error: :9:14: error: explicit specialization in non-namespace scope 'class DecideType' template class DecideType { template
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
3
votes
1 answer

How to explicitly specialize a function template from within another namespace?

For readability reasons, I would like to specialize a function template close to the definition of a class which is declared inside a namespace: #include template void my_function() { std::cout << "my_function default" <<…