Re: Are local structs illegal?
On 10/12/2010 1:43 PM, DeMarcus wrote:
Hi!
I'm using gcc 4.4.3. Is following illegal?
#include <vector>
void fnc()
{
struct MyStruct
{
MyStruct() {}
int someInt;
};
std::vector<MyStruct> vec;
// Maybe do something with vec.
}
int main()
{
fnc();
return 0;
}
From above I get the following error message at the std::vector line.
In function ?void fnc()?:
error: template argument for ?template<class _Tp> class std::allocator?
uses local type ?fnc()::MyStruct?
error: trying to instantiate ?template<class _Tp> class std::allocator?
error: template argument 2 is invalid
error: invalid type in declaration before ?;? token
Are local structs illegal?
No, but you can't use one to instantiate a template.
Can't remember chapter and verse, though.
{ Mod "chapter and verse"-factoid: C++98 ?14.3.1/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." -mod/aps }
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]