Partial Template specialization definition outside of class definition

djgandy

I am having a little trouble with a template specialization. I have looked for other answers, and thought I found the solution in this thread - Partial template specialization outside class definition - however it turns out that does not solve my problem.

I am trying to do some template specialization based on enum values to remove the need for unnecessary run-time polymorphism. When I define the template functions within the class body, it works OK, but when I move the definitions outside of the class template the compiler cannot match the signature.

My actual scenario is interfacing with an API that uses named objects for which each class of objects I am representing with an enum value. The objects are not directly related to each other, but they have very similar resource management / manipulation mechanisms. I initially tried using traits, but because I sometimes need to use completely different function signatures, traits didn't work out as I hoped.

Anyway, here is a cut down example of the problem I am facing.

Dog bark works, because it is defined in the class definition, but Cat meow does not, because it cannot find a declaration of meow. If I merge the definition and declaration cat meow will work.

Is there any way to partially specify templates in this manner? The reason being is that I'd like the dependency on external API's kept to a source file rather than in the header file.

enum class AnimalType { Dog, Cat, };
template<AnimalType Type> struct A;

template<>
struct A<AnimalType::Dog>
{
    // OK
    void bark() { std::cout << "woof"; }
};

template<>
struct A<AnimalType::Cat>
{
    void meow();
};

// Cannot match
template <>
void A<AnimalType::Cat>::meow()
{
}

GCC 4.9 complains

scratchpad/animal.h:105: error: template-id 'meow<>' for 'void <(AnimalType)1>::meow()' does not match any template declaration void A<AnimalType::Cat>::meow()

Thanks

Useless

This:

template<>
struct A<AnimalType::Cat> {
    void meow();
};

declares a template specialization with a non-template method. But this:

template <> void A<AnimalType::Cat>::meow()

implies specialization of a template method. Since A<AnimalType::Cat> is already fully-specialized, it doesn't need the template <> here.

Use this instead:

void A<AnimalType::Cat>::meow() {
    std::cout << "meow\n";
}

and consider for comparison this template method:

template<>
struct A<AnimalType::Cat> {
    template <AnimalType> void greet();
};
template <>
void A<AnimalType::Cat>::greet<AnimalType::Cat>() {
    std::cout << "purr\n";
}
template <>
void A<AnimalType::Cat>::greet<AnimalType::Dog>() {
    std::cout << "flee\n";
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Partial Template specialization definition outside of class definition

From Dev

Template specialization of constructor within class definition

From Dev

C++, partial specialization of 2-argument class template: unable to match function definition to an existing declaration

From Dev

Specialization of template <class T> definition of template <class T>

From Dev

inner class method definition outside of a template class

From Dev

Define operator[]() (array subscription) for template class outside of class definition

From Dev

Template class partial specialization syntax

From Dev

Double partial template specialization for a class

From Dev

Template class partial specialization syntax

From Dev

Move method definition for template nested class outside declaration

From Dev

Cannot use a member typedef of one class as a template parameter in a template specialization definition

From Dev

friend not allowed outside of a class definition

From Dev

Variable assignment outside class definition

From Dev

Move function outside class definition

From Dev

In template class member function body outside class definition, when are template parameters required?

From Dev

Partial class template specialization c++11

From Dev

What kind of class template definition is this?

From Dev

How do I define a template member function outside of a full specialized template class's definition?

From Dev

How do I define a template member function outside of a full specialized template class's definition?

From Dev

declaration of templated function (over container type) inside class and definition of it outside a template class over container type-

From Dev

Template template partial specialization failure: "expected a class template"

From Dev

Partial specialization of type parameter in template class with type & template template parameters

From Dev

Template static definition and explicit specialization instantiation errors in MSVC

From Dev

static_assert inside/outside class definition

From Dev

TypeScript - How to add a method outside the class definition

From Dev

Reference instance method outside class definition

From Dev

TypeScript - How to add a method outside the class definition

From Dev

Understanding Template Partial Specialization

From Dev

Partial template specialization for type

Related Related

  1. 1

    Partial Template specialization definition outside of class definition

  2. 2

    Template specialization of constructor within class definition

  3. 3

    C++, partial specialization of 2-argument class template: unable to match function definition to an existing declaration

  4. 4

    Specialization of template <class T> definition of template <class T>

  5. 5

    inner class method definition outside of a template class

  6. 6

    Define operator[]() (array subscription) for template class outside of class definition

  7. 7

    Template class partial specialization syntax

  8. 8

    Double partial template specialization for a class

  9. 9

    Template class partial specialization syntax

  10. 10

    Move method definition for template nested class outside declaration

  11. 11

    Cannot use a member typedef of one class as a template parameter in a template specialization definition

  12. 12

    friend not allowed outside of a class definition

  13. 13

    Variable assignment outside class definition

  14. 14

    Move function outside class definition

  15. 15

    In template class member function body outside class definition, when are template parameters required?

  16. 16

    Partial class template specialization c++11

  17. 17

    What kind of class template definition is this?

  18. 18

    How do I define a template member function outside of a full specialized template class's definition?

  19. 19

    How do I define a template member function outside of a full specialized template class's definition?

  20. 20

    declaration of templated function (over container type) inside class and definition of it outside a template class over container type-

  21. 21

    Template template partial specialization failure: "expected a class template"

  22. 22

    Partial specialization of type parameter in template class with type & template template parameters

  23. 23

    Template static definition and explicit specialization instantiation errors in MSVC

  24. 24

    static_assert inside/outside class definition

  25. 25

    TypeScript - How to add a method outside the class definition

  26. 26

    Reference instance method outside class definition

  27. 27

    TypeScript - How to add a method outside the class definition

  28. 28

    Understanding Template Partial Specialization

  29. 29

    Partial template specialization for type

HotTag

Archive