I have a class like this:
template<char _character>
class Foo
{
...
public:
static const char character = _character;
};
Is there a way I can access the _character parameter outside the class, without the static to forward it? Something like Foo::_character.
Short answer is no, you can’t.
_characteris the template parameter, and is unknown until you instantiate the template.After instantiation
_characteris no longer a member of your concrete instantiation, but rather thechar you passed in is.
By creating
static const char character = _character;you are creating a char data member which is dependent on the template parameter used to instantiate your class template.You can now access said data member from an instantiated class template:
Once you instantiate the class template,
Foo<'c'>::_characterdoesn’t exist.