Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does creating a local type vector fail

Tags:

c++

#include <iostream>
#include <vector>

int main()
{
    class Int {
        public:
            Int(int _i) : i(i) {}
        private:
            int i;
    };

    std::vector<Int> VI;
}

I try to compile the above code and got the following error message:

foo.cc: In function 'int main()':
foo.cc:13: error: 'main()::Int' uses local type 'main()::Int'
foo.cc:13: error:   trying to instantiate 'template<class _Alloc> class std::allocator'
foo.cc:13: error: template argument 2 is invalid
foo.cc:13: error: invalid type in declaration before ';' token

Could anyone of you tell me why I can't do things like this in C++? Thanks in advance.

like image 741
Haiyuan Zhang Avatar asked Jul 06 '10 09:07

Haiyuan Zhang


1 Answers

The standard explictly prohibits using local classes to instantiate templates in 14.3.1[temp.arg.type]/2.

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

This will be changed in C++0x.

like image 141
David Rodríguez - dribeas Avatar answered Nov 01 '22 17:11

David Rodríguez - dribeas