Prev: Partial Specialization of Template Function
Next: pros and cons of returning const ref to string instead of string by value
From: Kris Prad on 4 Dec 2009 02:08 I have no particular reason to do this, but consider the instantiation of this: template<int N> struct A { A() { cout << "Value of N is: " << N << endl; } }; A<__LINE__> a; // does not compile in VS2008 // But compiles in Comeau online. // This one compiles in both static const int LINE = __LINE__; A<LINE> a; Preprocessor is not part of the standard, so both are correct? VS2008, however, appears to be inconsistent. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Chris Morley on 5 Dec 2009 07:06 > I have no particular reason to do this, but consider the instantiation > of this: > > template<int N> struct A > { > A() > { > cout << "Value of N is: " << N << endl; > } > }; > > A<__LINE__> a; // does not compile in VS2008 > // But compiles in Comeau online. > > // This one compiles in both > static const int LINE = __LINE__; > A<LINE> a; > > Preprocessor is not part of the standard, so both are correct? VS2008, > however, appears to be inconsistent. You need some preprocessor trickery. Quick adaption of the macros I use to produce line numbered temp variables (useful within other macros so you don't get variable name clashes) appends zero onto the front of line & feeds that to the foo<>. This compiles fine in VS2008. #include <stdio.h> #define _LINENAME_CAT( name, line ) name##line #define _LINENAME( name, line ) _LINENAME_CAT( name, line ) //#define LINED( name ) _LINENAME( name, __LINE__ ) #define LINED _LINENAME( 0, __LINE__ ) template< int N > struct foo { void print() {printf("My number is: %d\n",N);} }; int main(int argc, char**argv) { foo< LINED > a; foo< LINED > b; a.print(); b.print(); } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Kenneth 'Bessarion' Boyd on 6 Dec 2009 03:21
On Dec 4, 1:08 pm, Kris Prad <krisp...(a)yahoo.co.uk> wrote: > Preprocessor is not part of the standard, so both [MSVC and Comeau] are correct? The preprocessor gets an entire chapter of both the C++ and C standards, as well as explicit mention in the stages of compilation. However, in many aspects Microsoft C++ and C compiliers appear to have feature-frozen or architecture-frozen between 1990 and 1999. > VS2008, however, appears to be inconsistent. VS2008 is failing to implement C++98 and higher. [section lex.phases; C++98 2.1/C++0X n3000 2.2 are materially identical]. VS2008 evidently is trying to interpret templates (part of stage 7) before logically completing handing preprocessing (stage 4). I assume VS2008 is implementing some pre-standard draft of C++ in regard to when templates are parsed, but can't verify which one quickly [do not have the relevant drafts downloaded]. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |