[boost] __COUNTER__ vs __LINE__

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

[boost] __COUNTER__ vs __LINE__

lcaminiti
Hello all,

Many of Boost macro APIs PP_CAT symbols with either __LINE__ or __COUNTER__ to make them unique (MPL_STATIC_ASSERT, SCOPE_EXIT, etc). AFAIK, __LINE__ is C++03 standard while __COUNTER__ is only a MSVC extension. I think using __COUNTER__ instead of __LINE__ might allow to expand a macro multiple times on the same line... but a  part from that:

What's the reason for using __COUNTER__ on MSVC instead of just using __LINE__ for all compilers?

Thanks a lot.
--Lorenzo
Reply | Threaded
Open this post in threaded view
|

Re: __COUNTER__ vs __LINE__

Christopher Jefferson

On 11 Jan 2012, at 18:28, lcaminiti wrote:

> Hello all,
>
> Many of Boost macro APIs PP_CAT symbols with either __LINE__ or __COUNTER__
> to make them unique (MPL_STATIC_ASSERT, SCOPE_EXIT, etc). AFAIK, __LINE__ is
> C++03 standard while __COUNTER__ is only a MSVC extension. I think using
> __COUNTER__ instead of __LINE__ might allow to expand a macro multiple times
> on the same line... but a  part from that:
>
> What's the reason for using __COUNTER__ on MSVC instead of just using
> __LINE__ for all compilers?

__LINE__ is restarted for each header file separately, so you can get problems with using a macro on the same line number of different files. I've been bitten by this before. __COUNTER__ is superior.

Chris

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: __COUNTER__ vs __LINE__

Andrey Semashev-2
In reply to this post by lcaminiti
On Wednesday, January 11, 2012 10:28:25 lcaminiti wrote:

> Hello all,
>
> Many of Boost macro APIs PP_CAT symbols with either __LINE__ or __COUNTER__
> to make them unique (MPL_STATIC_ASSERT, SCOPE_EXIT, etc). AFAIK, __LINE__ is
> C++03 standard while __COUNTER__ is only a MSVC extension. I think using
> __COUNTER__ instead of __LINE__ might allow to expand a macro multiple
> times on the same line... but a  part from that:
>
> What's the reason for using __COUNTER__ on MSVC instead of just using
> __LINE__ for all compilers?

MSVC has problems with expanding __LINE__ when precompiled headers are used.
Basically, __COUNTER__ is the only practical option with this compiler when
the macro is to be expanded in a header.


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: __COUNTER__ vs __LINE__

Mathias Gaunard-2
In reply to this post by Christopher Jefferson
On 01/11/2012 07:44 PM, Christopher Jefferson wrote:

>
> On 11 Jan 2012, at 18:28, lcaminiti wrote:
>
>> Hello all,
>>
>> Many of Boost macro APIs PP_CAT symbols with either __LINE__ or __COUNTER__
>> to make them unique (MPL_STATIC_ASSERT, SCOPE_EXIT, etc). AFAIK, __LINE__ is
>> C++03 standard while __COUNTER__ is only a MSVC extension. I think using
>> __COUNTER__ instead of __LINE__ might allow to expand a macro multiple times
>> on the same line... but a  part from that:
>>
>> What's the reason for using __COUNTER__ on MSVC instead of just using
>> __LINE__ for all compilers?
>
> __LINE__ is restarted for each header file separately, so you can get problems with using a macro on the same line number of different files. I've been bitten by this before. __COUNTER__ is superior.

__COUNTER__ is only per TU, and careless usage can easily lead to ODR
violations.


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: __COUNTER__ vs __LINE__

Alf P. Steinbach
In reply to this post by Andrey Semashev-2
On 11.01.2012 20:06, Andrey Semashev wrote:

> On Wednesday, January 11, 2012 10:28:25 lcaminiti wrote:
>> Hello all,
>>
>> Many of Boost macro APIs PP_CAT symbols with either __LINE__ or __COUNTER__
>> to make them unique (MPL_STATIC_ASSERT, SCOPE_EXIT, etc). AFAIK, __LINE__ is
>> C++03 standard while __COUNTER__ is only a MSVC extension. I think using
>> __COUNTER__ instead of __LINE__ might allow to expand a macro multiple
>> times on the same line... but a  part from that:
>>
>> What's the reason for using __COUNTER__ on MSVC instead of just using
>> __LINE__ for all compilers?
>
> MSVC has problems with expanding __LINE__ when precompiled headers are used.
> Basically, __COUNTER__ is the only practical option with this compiler when
> the macro is to be expanded in a header.

As I recall, the problem is when the option for edit-and-continue is used.

I had that problem with Petru Marginean's original ScopeGuard code.

With the edit-and-continue option, Visual C++ really messed up the
explansion of __LINE__, so that the ScopeGuard temporary names became
invalid.


Cheers,

- Alf


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: __COUNTER__ vs __LINE__

lcaminiti
In reply to this post by Andrey Semashev-2
Andrey Semashev-2 wrote
On Wednesday, January 11, 2012 10:28:25 lcaminiti wrote:
> Hello all,
>
> Many of Boost macro APIs PP_CAT symbols with either __LINE__ or __COUNTER__
> to make them unique (MPL_STATIC_ASSERT, SCOPE_EXIT, etc). AFAIK, __LINE__ is
> C++03 standard while __COUNTER__ is only a MSVC extension. I think using
> __COUNTER__ instead of __LINE__ might allow to expand a macro multiple
> times on the same line... but a  part from that:
>
> What's the reason for using __COUNTER__ on MSVC instead of just using
> __LINE__ for all compilers?

MSVC has problems with expanding __LINE__ when precompiled headers are used.
Basically, __COUNTER__ is the only practical option with this compiler when
the macro is to be expanded in a header.
OK. Is there a Boost macro like this already?

#include <boost/config.hpp>

#ifdef BOOST_MSVC
#   define BOOST_LINE_COUNTER __COUNTER__
#else
#   define BOOST_LINE_COUNTER __LINE__
#endif

Thanks.
--Lorenzo
Reply | Threaded
Open this post in threaded view
|

Re: __COUNTER__ vs __LINE__

Kim Barrett-2
On Jan 13, 2012, at 2:16 PM, lcaminiti wrote:
> OK. Is there a Boost macro like this already?
>
> #include <boost/config.hpp>
>
> #ifdef BOOST_MSVC
> #   define BOOST_LINE_COUNTER __COUNTER__
> #else
> #   define BOOST_LINE_COUNTER __LINE__
> #endif


__COUNTER__ is also available in gcc4.3 and later.

http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html#Common-Predefined-Macros

I don't know how precisely it matches the MSVC __COUNTER__ in various cases like pre-compiled headers and the like.



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost