Functions that evaluate to constants
Hi, I realize that C++03 will not permit functions to evaluate down to compile-time constants. However, I cannot wait until C++0x for this feature. So, I'm hoping that Boost will allow a temporary solution. Consider the following structure: static struct nullptr_t { template< typename t_type > operator t_type*() { return 0; } } nullptr; The compiler has everything it needs to turn this code into a single integral constant value. So, if I do the following: int* foo; if( foo == nullptr ) { } Then it should evaluate down to: int* foo; if( foo == 0 ) { } Is there any way to provide this behavior in Boost? I would like to avoid altering the actual design if possible. Perhaps this actually *does* evaluate to a constant and the problem is that I'm actually unaware of some specific rule that I didn't find in the C++03 standard. Thanks.
I would like to refer you to this address:
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr
Is there any chance tha in your tests int* foo is a member variable? Which
compiler do you use?
The example from the location above compiles fine, except the line:
const int n = 0;
if (nullptr == n) {} // ok
Which IMO is a mistake in the wiki page.
With Kind Regards,
Ovanes Markarian
On Mon, Mar 31, 2008 at 6:57 PM, Robert Dailey
Hi,
I realize that C++03 will not permit functions to evaluate down to compile-time constants. However, I cannot wait until C++0x for this feature. So, I'm hoping that Boost will allow a temporary solution. Consider the following structure:
static struct nullptr_t { template< typename t_type > operator t_type*() { return 0; } } nullptr;
The compiler has everything it needs to turn this code into a single integral constant value. So, if I do the following:
int* foo; if( foo == nullptr ) { }
Then it should evaluate down to:
int* foo; if( foo == 0 ) { }
Is there any way to provide this behavior in Boost? I would like to avoid altering the actual design if possible. Perhaps this actually *does* evaluate to a constant and the problem is that I'm actually unaware of some specific rule that I didn't find in the C++03 standard. Thanks.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Sorry, my current compiler is VC++ 7.1
On Mon, Mar 31, 2008 at 7:21 PM, Ovanes Markarian
I would like to refer you to this address:
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr
Is there any chance tha in your tests int* foo is a member variable? Which compiler do you use?
The example from the location above compiles fine, except the line:
const int n = 0; if (nullptr == n) {} // ok
Which IMO is a mistake in the wiki page.
With Kind Regards, Ovanes Markarian
On Mon, Mar 31, 2008 at 6:57 PM, Robert Dailey
wrote: Hi,
I realize that C++03 will not permit functions to evaluate down to compile-time constants. However, I cannot wait until C++0x for this feature. So, I'm hoping that Boost will allow a temporary solution. Consider the following structure:
static struct nullptr_t { template< typename t_type > operator t_type*() { return 0; } } nullptr;
The compiler has everything it needs to turn this code into a single integral constant value. So, if I do the following:
int* foo; if( foo == nullptr ) { }
Then it should evaluate down to:
int* foo; if( foo == 0 ) { }
Is there any way to provide this behavior in Boost? I would like to avoid altering the actual design if possible. Perhaps this actually *does* evaluate to a constant and the problem is that I'm actually unaware of some specific rule that I didn't find in the C++03 standard. Thanks.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Sorry again I misread your post. As Steven already posted if you optimize
for speed compiler will optimize this down to const.
If you don't like the idea of checking the generated assembler and you use
visual studio. You can compile in release mode with debug infos enabled.
Then you can step through your functionality and see what was inlined. This
is for sure not a guarantee that compiler will always inline or optimize
this code, but most likely it will.
With Kind Regards,
Ovanes
On Mon, Mar 31, 2008 at 7:21 PM, Ovanes Markarian
Sorry, my current compiler is VC++ 7.1
On Mon, Mar 31, 2008 at 7:21 PM, Ovanes Markarian
wrote: I would like to refer you to this address:
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/nullptr
Is there any chance tha in your tests int* foo is a member variable? Which compiler do you use?
The example from the location above compiles fine, except the line:
const int n = 0; if (nullptr == n) {} // ok
Which IMO is a mistake in the wiki page.
With Kind Regards, Ovanes Markarian
On Mon, Mar 31, 2008 at 6:57 PM, Robert Dailey
wrote: Hi,
I realize that C++03 will not permit functions to evaluate down to compile-time constants. However, I cannot wait until C++0x for this feature. So, I'm hoping that Boost will allow a temporary solution. Consider the following structure:
static struct nullptr_t { template< typename t_type > operator t_type*() { return 0; } } nullptr;
The compiler has everything it needs to turn this code into a single integral constant value. So, if I do the following:
int* foo; if( foo == nullptr ) { }
Then it should evaluate down to:
int* foo; if( foo == 0 ) { }
Is there any way to provide this behavior in Boost? I would like to avoid altering the actual design if possible. Perhaps this actually *does* evaluate to a constant and the problem is that I'm actually unaware of some specific rule that I didn't find in the C++03 standard. Thanks.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Mon, Mar 31, 2008 at 1:06 PM, Ovanes Markarian
Sorry again I misread your post. As Steven already posted if you optimize for speed compiler will optimize this down to const.
If you don't like the idea of checking the generated assembler and you use visual studio. You can compile in release mode with debug infos enabled. Then you can step through your functionality and see what was inlined. This is for sure not a guarantee that compiler will always inline or optimize this code, but most likely it will.
With Kind Regards, Ovanes
Thanks for the help guys. For some odd reason I forgot about inlining when I originally made my inquiry. My head has been running in circles all day, I apologize for the way I phrased my question. I actually already knew this stuff but I had a brain fart.
Monday is always a hard day ;)
On Mon, Mar 31, 2008 at 9:30 PM, Robert Dailey
On Mon, Mar 31, 2008 at 1:06 PM, Ovanes Markarian
wrote: Sorry again I misread your post. As Steven already posted if you optimize for speed compiler will optimize this down to const.
If you don't like the idea of checking the generated assembler and you use visual studio. You can compile in release mode with debug infos enabled. Then you can step through your functionality and see what was inlined. This is for sure not a guarantee that compiler will always inline or optimize this code, but most likely it will.
With Kind Regards, Ovanes
Thanks for the help guys. For some odd reason I forgot about inlining when I originally made my inquiry. My head has been running in circles all day, I apologize for the way I phrased my question. I actually already knew this stuff but I had a brain fart.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
AMDG Robert Dailey wrote:
Hi,
I realize that C++03 will not permit functions to evaluate down to compile-time constants. However, I cannot wait until C++0x for this feature. So, I'm hoping that Boost will allow a temporary solution. Consider the following structure:
static struct nullptr_t { template< typename t_type > operator t_type*() { return 0; } } nullptr;
The compiler has everything it needs to turn this code into a single integral constant value. So, if I do the following:
int* foo; if( foo == nullptr ) { }
Then it should evaluate down to:
int* foo; if( foo == 0 ) { }
Is there any way to provide this behavior in Boost? I would like to avoid altering the actual design if possible. Perhaps this actually *does* evaluate to a constant and the problem is that I'm actually unaware of some specific rule that I didn't find in the C++03 standard. Thanks.
If you don't need a constant expression, the compiler will probably figure it out if you compile with optimization. In Christ, Steven Watanabe
participants (3)
-
Ovanes Markarian
-
Robert Dailey
-
Steven Watanabe