5

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52

1 Answers1

11
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).

Community
  • 1
  • 1
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • Thanks a lot man. That makes it clear although its not my code and I am not altogether clear on what I can do as a quick workaround – Tinashe Mutsvangwa Oct 07 '13 at 13:24
  • @TinasheMutsvangwa glad to have been of help! – TemplateRex Oct 07 '13 at 13:29
  • @TinasheMutsvangwa The quick workaround would be to remove the typename in those two last typedefs. Although it's not your code, is there anything preventing you from editing it ? – François Moisan Oct 07 '13 at 15:09
  • @FrançoisMoisan: I can edit the code but I am not sure how it affects the rest. I have tried your advise and there are no compilation errors but I am still wondering what the difference will be in the execution – Tinashe Mutsvangwa Oct 08 '13 at 08:34