template declaration of `typedef typename Foo<T>::Bar Bar'

21,353

Solution 1

The typedef declaration in C++ cannot be a template. However, C++11 added an alternative syntax using the using declaration to allow parametrized type aliases:

template <typename T>
using Bar = typename Foo<T>::Bar;

Now you can use:

Bar<int> x;   // is a Foo<int>::Bar

Solution 2

You can't typedef a template. However, you can use an alias template. The code below demonstrates the use and fixed a few other problems, too:

template <class T>
class Foo
{
public:
    typedef T Bar;
};

template <class T>
using Bar =  typename Foo<T>::Bar;

int main(int argc, char *argv[])
{
    Bar<int> bar;
    Foo<int> foo;
}

Solution 3

typedef's cannot be templates. This is exactly the reason C++11 invented alias templates. Try

template <class T>
using Bar = typename Foo<T>::Bar;

Solution 4

Hope it's okay to add late answers...

I'm still using VS2012 which does not appear to implement alias templates, so I resorted to this concoction for my purposes:

//map<CWinThread*, AKTHREADINFO<T> > mapThreads;   // won't compile
#define MAP_THREADS(T) map<CWinThread*, AKTHREADINFO<T> >

...

MAP_THREADS(T) mapThreads;

Sorry it does not demonstrate the original example, but you get the drift.

Share:
21,353
geraldCelente
Author by

geraldCelente

Updated on October 26, 2020

Comments

  • geraldCelente
    geraldCelente over 3 years

    I am encountering great difficulty in declaring a templated type as shown below.

    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    
    template <class T>
    class Foo
    {
     typedef T Bar;
    };
    
    template <class T>
    typedef typename Foo<T>::Bar Bar;
    
    
    
    
    int main(int argc, char *argv[])
    {
    
        Bar bar;
    
        Foo<int> foo;
    
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    

    I get error

    template declaration of `typedef typename Foo<T>::Bar Bar' 
    

    about line

    template <class T>
    typedef typename Foo<T>::Bar Bar;
    

    I am doing this because I want avoid writing typename Foo::Bar throught my code.

    What am I doing wrong?

  • geraldCelente
    geraldCelente over 10 years
    Thank you! all of you have been equally helpful. I wish I could accept all three answer received. Anyway I will vote up all three answer.
  • geraldCelente
    geraldCelente over 10 years
    Thank you! all of you have been equally helpful. I wish I could accept all three answer received. Anyway I will vote up all three answer.
  • geraldCelente
    geraldCelente over 10 years
    Thank you! all of you have been equally helpful. I wish I could accept all three answer received. Anyway I will vote up all three answer.
  • Kerrek SB
    Kerrek SB over 10 years
    @geraldCelente: I don't think it matters or anyone would be upset -- pick whichever answer you find most useful :-) (Or toss a coin.)
  • Brian Jack
    Brian Jack over 9 years
    is there a library (eg: boost) that provides a "backaround" for compilers that are still falling off the C++11 bandwagon (like the gcc/mingw compilers)?