Re: vector<struct> cannot be compiled under gcc
fifth8118 wrote:
Hi, all:
I wrote following code, and compile it under gcc 3.4.3, but cannot be
compiled.
#include <vector>
#include <string>
using namespace std;
int main() {
struct stut {
float a;
string b;
};
vector <stut> d;
return 1;
}
gcc used to allow a local class to serve as a template type parameter -
an admittedly convenient feature especially when declaring functors for
std algorithms. Nevertheless the Standard does not allow a local class
to be used for this purpose - so support for local class template type
parameters in gcc has been dropped.
While others have suggested moving the class declaration for stut
outside of main() as the solution, doing so is likely just to replace
one problem with another. Presumably stut was declared a local class
for a reason - the reason being to keep its declaration out of the
global scope. So moving it back into global scope would reintroduce the
same problem for which declaring stut a local class had been the
solution.
Fortunately there is a solution that addresses both problems: that is,
to be able to use stut as a type parameter yet still ensure that it
remains "local" to its own source file. That solution is to declare
stut within an anonymous namespace at the global scope. An anonymous
namespace is both unique (thereby keeping stut local) but with external
linkage (meaning that stut qualifies for use as a template type
parameter).
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]