 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
John Torjo Guest
|
Posted: Tue Oct 26, 2004 10:51 pm Post subject: typedefing ahead |
|
|
Consider this code:
#include <iostream>
template<int i> struct test {
test() { std::cout << "general" << std::endl; }
};
typedef test<0> test_zero;
typedef test<1> test_one;
template<> struct test<0> {
test() { std::cout << "zero" << std::endl; }
};
int main() {
test_zero zero;
test_one one;
}
// output:
// zero
// general
In this case, test_zero, is an alias for test<0>, even though I later
specialize test<0>.
By reading the standard, it looks to me that this is perfectly legal,
and does exactly what it should.
(also, tested it with vc71, gcc3.3.1, como, and works)
Am I right about this - is this standard behavior?
Or is there some corner in the standard that forbids this?
Thanks.
Best,
John
John Torjo, Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/
-- v1.5 - tooltips at your fingertips (work for menus too!)
+ bitmap buttons, tab dialogs, hyper links, lite html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Wed Oct 27, 2004 2:03 pm Post subject: Re: typedefing ahead |
|
|
On 26 Oct 2004 18:51:36 -0400, [email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote:
| Quote: | Consider this code:
#include <iostream
template
test() { std::cout << "general" << std::endl; }
};
typedef test<0> test_zero;
typedef test<1> test_one;
template<> struct test<0> {
test() { std::cout << "zero" << std::endl; }
};
int main() {
test_zero zero;
test_one one;
}
// output:
// zero
// general
In this case, test_zero, is an alias for test<0>, even though I later
specialize test<0>.
|
Yes. A typedef is an alias, not a new name. It shouldn't cause test<0>
to become instantiated, AFAIK, because it hasn't yet been used.
| Quote: | By reading the standard, it looks to me that this is perfectly legal,
and does exactly what it should.
(also, tested it with vc71, gcc3.3.1, como, and works)
|
I will always vote with como <g>.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola Musatti Guest
|
Posted: Thu Oct 28, 2004 1:58 pm Post subject: Re: typedefing ahead |
|
|
[email]jtorjo (AT) yahoo (DOT) com[/email] (John Torjo) wrote in message news:<c638aac5.0410260117.2670aba6 (AT) posting (DOT) google.com>...
| Quote: | Consider this code:
#include <iostream
template
test() { std::cout << "general" << std::endl; }
};
typedef test<0> test_zero;
[...]
In this case, test_zero, is an alias for test<0>, even though I later
specialize test<0>.
By reading the standard, it looks to me that this is perfectly legal,
and does exactly what it should.
(also, tested it with vc71, gcc3.3.1, como, and works)
|
I can't give you a final answer, but considering that the following is valid:
template<int i> struct test;
typedef test<0> test_zero;
I'm pretty sure that what you do is just as valid.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|