given:
how does this work with templates?Code:struct A {
} a;
Printable View
given:
how does this work with templates?Code:struct A {
} a;
Code:temaplate<class T>
struct A
{
T data;
};
int main( )
{
A<int> a; //You can change the data type to basically anything you want here at object creation
//It will change the type qualifier for the 'data' struct variable
return 0;
}
Heh, well you could aways try testing it ;) Some templates actually require a constructor and destructor to compile. Other than that, I believe an empty structure takes up one byte of space (please someone jump in with a correction).
[edit]
I guess I misunderstood what you meant.
[/edit]
I suppose I'm being too vague here.
consider:
I want to use the same syntax with templates.Code:typedef struct foo { } bar, baz;
foo f;
bar r;
baz z;
Code:template <typename T>
class Foo {
T x_;
public:
Foo(){};
const T& x() const { return x_; }
};
typedef Foo<int> FooInt;
FooInt Fi;
ok, here's what I was hoping for:
in other words, typedef the template itself - not a particular parameterization of the template.Code:template <class T> typedef struct foo { } bar, baz;
foo <int> f;
bar <int> r;
baz <int> z;
Well thats done by default:
Unless do you also want to be able to call foo bar and baz?Code:template <typename T> struct foo { /* stuff */};
foo<int> f;
Well you can do this..
Example:
But if you are just wanting foo and bar and baz to be the same structure, why not use macros? You will lose compile time information since the compiler will see bar and baz as just foo in disguise. I'm really not sure why you are wanting to do this.Code:template <typename T>
struct foo {
};
typedef foo<int> bar;
>> But if you are just wanting foo and bar and baz to be the same structure, why not use macros?
I'd rather not use macros at all - I was just hoping there was some obscure feature of the language that would allow you to do this.
>> I'm really not sure why you are wanting to do this.
why does anyone want to typedef anything? maybe because it's easier to type? ;)
You can't have typedef templates, so what you're trying to do is impossible. Sorry. Basically, in order to allow your idea, this would need to be possible:
Admittedly, that would allow cool stuff:Code:template <typename T> typedef vector<T> dynarray<T>;
But it doesn't exist. It is being discussed as a language extension, but there is some weird stuff that needs to be resolved, especially when it comes to function parameter deduction.Code:template <typename T> typedef vector<vector<vector<T> > > vec3d<T>;
It's very hard to correctly deduct the type here.Code:template <typename T> typedef T CT<T>;
template <typename T>
void whee(CT<T> arg);
As a workaround for now, you can use nested typedefs:
Code:template <typename T>
struct vec3d
{
typedef vector<vector<vector<T> > > t;
};
vec3d<int>::t myvec;
I never put much thought into it, but yeah it would be handy. I have had instances where I've had to ABCs that are fundamentally the same template, but for the sake of neatness, they had different names. But even then I can't remember when i've made to mirror templates before. But perhaps thats because nothing like you are suggesting here exists ;)