tags:

views:

38

answers:

1

I have a class foo with an enum template parameter and for some reason it links to two versions of the ctor in the cpp file.

enum Enum
{
    bar,
    baz
};

template <Enum version = bar>
class foo
{
public:
    foo();
};

// CPP File
#include "foo.hpp"

foo<bar>::foo() { cout << "bar"; }
foo<baz>::foo() { cout << "baz"; }

I'm using msvc 2008, is this the standard behavior?
Are only type template parameters cannot be linked to cpp files?

+2  A: 

You are specializing both forms of the contstructor. Why are you surprised it links both forms in?

Goz
Maybe because one isn't referenced?
sbi
Because usually when I link to a template class it's members cannot be implemented in the cpp file.
the_drow
@sbi: Just because it isn't referenced doesn't mean it should be stripped. The compiler "may" strip it but its by no means guaranteed.@the_drow: You can implement members in a CPP file if you are specialising the template.
Goz
Behavior for that code is undefined. You need to declare the specialization in the header file. And you missed the `template<>` prefix.
Johannes Schaub - litb