given:
how does this work with templates?Code:struct A { } a;
given:
how does this work with templates?Code:struct A { } a;
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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; }
Last edited by The Brain; 12-19-2004 at 06:31 PM.
- "Problem Solving C++, The Object of Programming" -Walter Savitch
- "Data Structures and Other Objects using C++" -Walter Savitch
- "Assembly Language for Intel-Based Computers" -Kip Irvine
- "Programming Windows, 5th edition" -Charles Petzold
- "Visual C++ MFC Programming by Example" -John E. Swanke
- "Network Programming Windows" -Jones/Ohlund
- "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
- "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel
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]
Last edited by master5001; 12-19-2004 at 06:31 PM.
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:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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;
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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;
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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