The title is pretty self-explanatory, but here's a simplified example:
#include <cstdio>
template <typename T>
struct MyTemplate {
T member;
void printMemberSize() {
printf("%i\n", sizeof(T));
}
};
int main() {
MyTemplate<struct { int a; int b; }> t; // <-- compiler doesn't like this
t.printMemberSize();
return 0;
}
The compiler complains when I try to use an anonymous struct as a template argument. What's the best way to achieve something like this without having to have a separate, named struct definition?
The entities of variable, function, struct, and class can have templates.
Non-type template arguments are normally used to initialize a class or to specify the sizes of class members. For non-type integral arguments, the instance argument matches the corresponding template parameter as long as the instance argument has a value and sign appropriate to the parameter type.
1) A template template parameter with an optional name. 2) A template template parameter with an optional name and a default. 3) A template template parameter pack with an optional name.
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
You are not allowed to define an unnamed type as a template argument in C++03 or even in C++0x.
The best you can do it to create a named struct local to main (in C++0x1)
1 : You are not allowed to use a local type as template argument in C++03, however C++0x allows it.
Also check out the Defect Report here. The proposed solution mentions
The following types shall not be used as a template-argument for a template type-parameter:
- a type whose name has no linkage
- an unnamed class or enumeration type that has no name for linkage purposes (7.1.3 [dcl.typedef])
- a cv-qualified version of one of the types in this list
- a type created by application of declarator operators to one of the types in this list
- a function type that uses one of the types in this list
The compiler complains when I try to use an anonymous struct as a template parameter.
Did you mean template argument? Template parameter is different from template argument.
For example
template < typename T > // T is template parameter
class demo {};
int main()
{
demo <int> x; // int is template argument
}
Your problem isn't that the struct is unnamed, it's that the struct is declared locally. Using local types as template arguments is not permitted in C++03. It will be in C++0x though, so you might try upgrading your compiler.
EDIT: Actually, your problem is that inside a template argument list isn't a legal place to put a class definition, with or without a name, according to the C++ standard.
litb points out that although it fits into the C++0x grammar, defining a type here is forbidden by [dcl.type]
:
A type-specifier-seq shall not define a class or enumeration unless it appears in the type-id of an alias-declaration (7.1.3) that is not the declaration of a template-declaration.
simple-template-id:
template-name < template-argument-list_opt >
template-argument-list:
template-argument ..._opt
template-argument-list , template-argument ..._opt
template-argument:
constant-expression
type-id
id-expression
type-id:
type-specifier-seq abstract-declarator_opt
type-specifier-seq:
type-specifier attribute-specifier-seq_opt
type-specifier type-specifier-seq
type-specifier:
trailing-type-specifier
class-specifier
enum-specifier
class-specifier:
class-head { member-specification_opt }
For a while I had a question about typedef names, but litb cleared that up. They are allowed as template arguments via:
trailing-type-specifier:
simple-type-specifier
elaborated-type-specifier
typename-specifier
cv-qualifier
simple-type-specifier:
:: opt nested-name-specifier_opt type-name
:: opt nested-name-specifier template simple-template-id
char
char16_t
char32_t
wchar_t
bool
short
int
long
signed
unsigned
float
double
void
auto
decltype-specifier
type-name:
class-name
enum-name
typedef-name
simple-template-id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With