Cryptoman
Cryptoman

Reputation: 63

Incomplete Type Error when defining traits of templated structure

I've got a struct that finds the traits of the typename of the input:

template< typename > struct cstr_traits;
template< > struct cstr_traits< const char* >
{
    typedef char cstr_trait;
};
template< > struct cstr_traits< const wchar_t* >
{
    typedef wchar_t cstr_trait;
};

When used, I get Type 'cstr_traits<char const(&)[4]> is incomplete'. Here is the usage:

cstr_traits< decltype( "Foo" ) >::cstr_trait

Any ideas? I already tried this:

template< > struct cstr_traits< const const ( & )[ ] >

Why is it not being deduced to use one of the specialized templates?

Upvotes: 0

Views: 86

Answers (1)

Michael Kenzel
Michael Kenzel

Reputation: 15941

The problem is that the type of a string literal is not "pointer to first character" but "array of N const characters". This should do the trick:

template <std::size_t N>
struct cstr_traits<const char (&)[N]>
{
    using cstr_trait = char;
};

Also, for the sake of completeness, you probably wanna not forget about char16_t and char32_t in whatever it is that you're trying to do…

Upvotes: 2

Related Questions