BOOST_PP_COUNTER compiler errors (MSVC8)
Hi,
I'm currently compiling my code in Visual Studio 2005. Below is the code
that utilizes BOOST_PP_COUNTER. Note that the goal here is to increment the
counter 1 time each time the CHECK_MATCH_AND_RETURN macro is used. This
allows me to make sure that I've got one macro call per enumeration member.
The problem I'm having is that I'm getting page after page of compiler error
and as far as I can tell I'm using the counter system as it was shown in the
code examples. Below I've also listed some of the compiler errors I'm
getting:
Compiler Errors:
error C2162: expected macro formal parameter
error C2065: 'include' : undeclared identifier
error C2882: 'boost' : illegal use of namespace identifier in expression
Code:
#include
I think I found the issue. It seems that you can't use the # symbol inside
of macros without it being treated as a preprocessor operator. I don't know
of any way to escape the # character to be treated as part of the macro
code. Is there a way I can increment the counter without doing the #include
statement? Perhaps there is a different implementation of a counter
somewhere that utilizes templates?
Thanks.
On Dec 20, 2007 3:51 PM, Robert Dailey
Hi,
I'm currently compiling my code in Visual Studio 2005. Below is the code that utilizes BOOST_PP_COUNTER. Note that the goal here is to increment the counter 1 time each time the CHECK_MATCH_AND_RETURN macro is used. This allows me to make sure that I've got one macro call per enumeration member. The problem I'm having is that I'm getting page after page of compiler error and as far as I can tell I'm using the counter system as it was shown in the code examples. Below I've also listed some of the compiler errors I'm getting:
Compiler Errors: error C2162: expected macro formal parameter error C2065: 'include' : undeclared identifier error C2882: 'boost' : illegal use of namespace identifier in expression
Code:
#include
#include /// Various camera actions that may be performed. /// These actions are usually a result of input events. enum CameraAction { MOVE_FORWARD = 0 , MOVE_BACKWARD , MOVE_LEFT , MOVE_RIGHT , MOVE_UP , MOVE_DOWN , ROTATE_RIGHT , ROTATE_LEFT , ROTATE_UP , ROTATE_DOWN , ROLL_LEFT , ROLL_RIGHT , INCREASE_ACCEL , DECREASE_ACCEL , ENABLE_LOOK , TOGGLE_LOOK , ENABLE_AUTO_MOVE , TOGGLE_AUTO_MOVE
/// @cond , NUM_CAMERAACTIONS , CAMERAACTION_NONE /// @endcond };
//========================================================================================= CameraAction StringToCameraAction( const std::string& str ) { // I'm using a macro here because I need the stringizing preprocessor operator (#) // to make the conditionals more manageable. By using this macro, I never have to explicitly // update the string comparison itself if the enumeration names ever change. #define CHECK_MATCH_AND_RETURN( action ) if( str == #action ) { return action; } #include BOOST_PP_UPDATE_COUNTER()
CHECK_MATCH_AND_RETURN( ROTATE_LEFT ) CHECK_MATCH_AND_RETURN( ROTATE_RIGHT ) CHECK_MATCH_AND_RETURN( ROTATE_UP ) CHECK_MATCH_AND_RETURN( ROTATE_DOWN ) CHECK_MATCH_AND_RETURN( MOVE_UP ) CHECK_MATCH_AND_RETURN( MOVE_DOWN ) CHECK_MATCH_AND_RETURN( MOVE_LEFT ) CHECK_MATCH_AND_RETURN( MOVE_RIGHT ) CHECK_MATCH_AND_RETURN( MOVE_FORWARD ) CHECK_MATCH_AND_RETURN( MOVE_BACKWARD ) CHECK_MATCH_AND_RETURN( ROLL_RIGHT ) CHECK_MATCH_AND_RETURN( ROLL_LEFT ) CHECK_MATCH_AND_RETURN( INCREASE_ACCEL ) CHECK_MATCH_AND_RETURN( DECREASE_ACCEL ) CHECK_MATCH_AND_RETURN( ENABLE_LOOK ) CHECK_MATCH_AND_RETURN( TOGGLE_LOOK ) CHECK_MATCH_AND_RETURN( ENABLE_AUTO_MOVE ) CHECK_MATCH_AND_RETURN( TOGGLE_AUTO_MOVE )
// This helps me remember to add/remove items above. BOOST_STATIC_ASSERT( (BOOST_PP_COUNTER)+1 != NUM_CAMERAACTIONS );
return CAMERAACTION_NONE; }
Hello Robert, Friday, December 21, 2007, 12:51:03 AM, you wrote: RD> I'm currently compiling my code in Visual Studio 2005. Below RD> is the code that utilizes BOOST_PP_COUNTER. Note that the goal RD> here is to increment the counter 1 time each time the RD> CHECK_MATCH_AND_RETURN macro is used. This allows me to make sure RD> that I've got one macro call per enumeration member. The problem RD> I'm having is that I'm getting page after page of compiler error RD> and as far as I can tell I'm using the counter system as it was RD> shown in the code examples. Below I've also listed some of the RD> compiler errors I'm getting: RD> //========================================================================================= RD> CameraAction StringToCameraAction( const std::string& str ) RD> { RD> // I'm using a macro here because I need the stringizing preprocessor operator (#) RD> // to make the conditionals more manageable. By using RD> this macro, I never have to explicitly RD> // update the string comparison itself if the enumeration names ever change. RD> #define CHECK_MATCH_AND_RETURN( action ) if( str == RD> #action ) { return action; } #include BOOST_PP_UPDATE_COUNTER() RD> CHECK_MATCH_AND_RETURN( ROTATE_LEFT ) RD> CHECK_MATCH_AND_RETURN( ROTATE_RIGHT ) You can't do that cause # is invalid preprocessor token. Instead you should explicitly write include lines: #define CHECK_MATCH_AND_RETURN( action ) if( str == #action ) { return action; } CHECK_MATCH_AND_RETURN( ROTATE_LEFT ) #include BOOST_PP_UPDATE_COUNTER() CHECK_MATCH_AND_RETURN( ROTATE_RIGHT ) #include BOOST_PP_UPDATE_COUNTER() etc. -- Best regards, Andry mailto:andry@inbox.ru
If I have to do it manually it pretty much defeats the purpose of using it
in the first place. I want it to be automated.
On Dec 20, 2007 4:44 PM, Andry
Hello Robert,
Friday, December 21, 2007, 12:51:03 AM, you wrote:
RD> I'm currently compiling my code in Visual Studio 2005. Below RD> is the code that utilizes BOOST_PP_COUNTER. Note that the goal RD> here is to increment the counter 1 time each time the RD> CHECK_MATCH_AND_RETURN macro is used. This allows me to make sure RD> that I've got one macro call per enumeration member. The problem RD> I'm having is that I'm getting page after page of compiler error RD> and as far as I can tell I'm using the counter system as it was RD> shown in the code examples. Below I've also listed some of the RD> compiler errors I'm getting:
RD> //========================================================================================= RD> CameraAction StringToCameraAction( const std::string& str ) RD> { RD> // I'm using a macro here because I need the stringizing preprocessor operator (#) RD> // to make the conditionals more manageable. By using RD> this macro, I never have to explicitly RD> // update the string comparison itself if the enumeration names ever change. RD> #define CHECK_MATCH_AND_RETURN( action ) if( str == RD> #action ) { return action; } #include BOOST_PP_UPDATE_COUNTER()
RD> CHECK_MATCH_AND_RETURN( ROTATE_LEFT ) RD> CHECK_MATCH_AND_RETURN( ROTATE_RIGHT ) You can't do that cause # is invalid preprocessor token. Instead you should explicitly write include lines:
#define CHECK_MATCH_AND_RETURN( action ) if( str == #action ) { return action; } CHECK_MATCH_AND_RETURN( ROTATE_LEFT ) #include BOOST_PP_UPDATE_COUNTER() CHECK_MATCH_AND_RETURN( ROTATE_RIGHT ) #include BOOST_PP_UPDATE_COUNTER()
etc.
-- Best regards, Andry mailto:andry@inbox.ru
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hello Robert,
Friday, December 21, 2007, 1:46:24 AM, you wrote:
RD> If I have to do it manually it pretty much defeats the
RD> purpose of using it in the first place. I want it to be automated.
You can download here
https://sourceforge.net/project/showfiles.php?group_id=198908&package_id=243070&release_id=552700
patch to boost 1.34.1 which adding to preprocessor some additional
functionality in which is BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER.
Purpose of this macro is assigning to slots builtin counters like
__COUNTER__, here the example:
//////////////////////////////////////////////////////////////////////////
#include
Thanks for your response. I'm actually completely confused as to what you're
doing. I notice you're using __COUNTER__, however couldn't I just use
__COUNTER__ without boost? What would the code look like for that? I don't
understand the need for this patch to the boost counter library.
On Dec 20, 2007 9:45 PM, Andry
Hello Robert,
Friday, December 21, 2007, 1:46:24 AM, you wrote:
RD> If I have to do it manually it pretty much defeats the RD> purpose of using it in the first place. I want it to be automated. You can download here
https://sourceforge.net/project/showfiles.php?group_id=198908&package_id=243070&release_id=552700 patch to boost 1.34.1 which adding to preprocessor some additional functionality in which is BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER. Purpose of this macro is assigning to slots builtin counters like __COUNTER__, here the example:
////////////////////////////////////////////////////////////////////////// #include
#include #include #define BUILTIN_COUNTER __COUNTER__
int builtin_counter_already_utilized_somewhere[] = { BUILTIN_COUNTER,BUILTIN_COUNTER,BUILTIN_COUNTER,BUILTIN_COUNTER, BUILTIN_COUNTER,BUILTIN_COUNTER,BUILTIN_COUNTER,BUILTIN_COUNTER, BUILTIN_COUNTER,BUILTIN_COUNTER };
//Remember and increment last build-in counter value. //WARNING: Slot #1 can be used by another (external) code (for example, by another included header), // you should exactly know that slot #1 doesn't used some else where between includes. #define BOOST_PP_VALUE BUILTIN_COUNTER #include BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER(1) //Evaluate BOOST_PP_VALUE as built-in counter and push it in slot #1
#define MYCOUNTER BOOST_PP_SUB(BUILTIN_COUNTER,BOOST_PP_SLOT(1))
int main() { int myarray[] = {MYCOUNTER,MYCOUNTER,MYCOUNTER,MYCOUNTER}; printf("builtin_counter_already_utilized_somewhere:\n"); for(int i = 0; i < sizeof(builtin_counter_already_utilized_somewhere)/sizeof(builtin_counter_already_utilized_somewhere[0]); i++) { printf("%d\n",builtin_counter_already_utilized_somewhere[i]); } printf("myarray:\n"); for(int i = 0; i < sizeof(myarray)/sizeof(myarray[0]); i++) { printf("%d\n",myarray[i]); } printf("__COUNTER__ = %u\n",BUILTIN_COUNTER); return 0; } //////////////////////////////////////////////////////////////////////////
You should use BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER instead BOOST_PP_ASSIGN_SLOT when defining BOOST_PP_VALUE to built-in counter macro.
In your code you should do something what: ////////////////////////////////////////////////////////////////////////// #define BOOST_PP_VALUE __COUNTER__ #include BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER(1)
#define CHECK_MATCH_AND_RETURN( action ) if( str == #action ) { return action; } void(__COUNTER__);
CHECK_MATCH_AND_RETURN( ROTATE_LEFT ) CHECK_MATCH_AND_RETURN( ROTATE_RIGHT ) CHECK_MATCH_AND_RETURN( ROTATE_UP ) CHECK_MATCH_AND_RETURN( ROTATE_DOWN ) CHECK_MATCH_AND_RETURN( MOVE_UP ) CHECK_MATCH_AND_RETURN( MOVE_DOWN ) CHECK_MATCH_AND_RETURN( MOVE_LEFT ) CHECK_MATCH_AND_RETURN( MOVE_RIGHT ) CHECK_MATCH_AND_RETURN( MOVE_FORWARD ) CHECK_MATCH_AND_RETURN( MOVE_BACKWARD ) CHECK_MATCH_AND_RETURN( ROLL_RIGHT ) CHECK_MATCH_AND_RETURN( ROLL_LEFT ) CHECK_MATCH_AND_RETURN( INCREASE_ACCEL ) CHECK_MATCH_AND_RETURN( DECREASE_ACCEL ) CHECK_MATCH_AND_RETURN( ENABLE_LOOK ) CHECK_MATCH_AND_RETURN( TOGGLE_LOOK ) CHECK_MATCH_AND_RETURN( ENABLE_AUTO_MOVE ) CHECK_MATCH_AND_RETURN( TOGGLE_AUTO_MOVE )
// This helps me remember to add/remove items above. BOOST_STATIC_ASSERT( BOOST_PP_SUB(__COUNTER__,BOOST_PP_SLOT(1))+1 != NUM_CAMERAACTIONS ); //////////////////////////////////////////////////////////////////////////
PS: GNU C/C++ recently supports __COUNTER__ macro from 4.3 version.
-- Best regards, Andry mailto:andry@inbox.ru
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hello Robert, RD> Thanks for your response. I'm actually completely confused as RD> to what you're doing. I notice you're using __COUNTER__, however RD> couldn't I just use __COUNTER__ without boost? What would the code RD> look like for that? Read documentation about slot usage here: http://boost.org/libs/preprocessor/doc/ref/slot.html and here: http://boost.org/libs/preprocessor/doc/index.html RD> I don't understand the need for this patch to the boost counter library. You can't use built-in counter __COUNTER__ with BOOST_PP_ASSIGN_SLOT, because it autoincrements in point of use. To avoid this patch adding BOOST_PP_ASSIGN_SLOT_BUILTIN_COUNTER macro which works correctly with __COUNTER__. For details read README_*.txt attached to patch. -- Best regards, Andry mailto:andry@inbox.ru
participants (2)
-
Andry
-
Robert Dailey