error C2899: typename cannot be used outside a template declaration

Tinashe Mutsvangwa picture Tinashe Mutsvangwa · Oct 7, 2013 · Viewed 7.1k times · Source

I am trying the following in MSV2010

namespace statismo {
template<>
struct RepresenterTraits<itk::Image<itk::Vector<float, 3u>, 3u> > {

    typedef itk::Image<itk::Vector<float, 3u>, 3u> VectorImageType;
    typedef VectorImageType::Pointer DatasetPointerType;
    typedef VectorImageType::Pointer DatasetConstPointerType;
    typedef typename VectorImageType::PointType PointType;
    typedef typename VectorImageType::PixelType ValueType;
};

I am getting the following error:

error C2899: typename cannot be used outside a template declaration

An help in a workaround would be greatly appreciated.

Answer

TemplateRex picture TemplateRex · Oct 7, 2013
namespace statismo {
template<>
struct RepresenterTraits<itk::Image<itk::Vector<float, 3u>, 3u> > {

    // bla

    typedef typename VectorImageType::PointType PointType;
            ^^^^^^^^

    typedef typename VectorImageType::PixelType ValueType;
            ^^^^^^^^
};

Your typename keywords are placed inside the explicit specalization of RepresenterTraits<T> for itk::Image<itk::Vector<float, 3u>, 3u>. However, this is a regular class, not a class template. This means that VectorImageType is not a dependent name and the compiler knows that PixelType is a nested type. That's why it's not allowed to use typename. See als this Q&A.

Note that in C++11, this restriction has been lifted, and the use of typename is allowed but not required in non-template contexts. See e.g. this example

#include <iostream>

template<class T>
struct V 
{ 
    typedef T type; 
};

template<class T>
struct S
{
    // typename required in C++98/C++11
    typedef typename V<T>::type type;    
};

template<>
struct S<int>
{
    // typename not allowed in C++98, allowed in C++11
    // accepted by g++/Clang in C++98 mode as well (not by MSVC2010)
    typedef typename V<int>::type type;    
};

struct R
{
    // typename not allowed in C++98, allowed in C++11
    // accepted by g++ in C++98 mode as well (not by Clang/MSVC2010)
    typedef typename V<int>::type type;    
};

int main()
{
}

Live Example (Just play with the g++/clang and std=c++98 / std=c++11 command line options).