C++what is the type of the __LINE__ macro

11,271

Solution 1

C++03 §16.8p1:

__LINE__ The line number of the current source line (a decimal constant).

This will either be int, or if INT_MAX (which is allowed to be as little as 32,767) is not big enough (… I won't ask …), then it will be long int. If it would be bigger than LONG_MAX, then you have undefined behavior, which, for once, is not a problem worth worrying about in a file of at least 2,147,483,647 lines (the minimum allowed value for LONG_MAX).

The same section also lists other macros you may be interested in.

Solution 2

The C++ standard simply has this to say:

__LINE__: The presumed line number (within the current source file) of the current source line (an integer constant).

It does not actually state the type so it's most likely going to be the same type as an unadorned integer would be in your source code which would be an int. The fact that the upper end of the allowed range is 2G - 1 supports that (even though the lower range is 1).

The fact that #line only allows digits (no trailing U to make it unsigned) can also be read to support this.

But, that's only support. I couldn't find a definitive statement within either the C++ or C standards. It just makes sense*a that it will be translated into something like 42 when it goes through the preprocessing phase and that's what the compiler will see, treating it exactly like 42 (an int).


*a: This wouldn't be the first time my common sense was wrong, though :-)

Solution 3

For general C++ code, see the other answer.

In Visual Studio 2017 (and, I suspect, all other versions) __LINE__ has type long.

I used the following code to discover it:

#include <iostream>
#include <typeinfo>

template <typename T>
void print_type(T x)
{
    std::cout << x << " has type " << typeid(x).name();
}

int main()
{
    print_type(__LINE__);
}
Share:
11,271
prabhakaran
Author by

prabhakaran

A newbie to qt,openGL,NetworkProgramming Know python,perl,shell scripting. A well versed(medium level) C++,linux,perl,c# programmer Crawling around javascript,xul and new technologies. I am a Linux lover got tricked into windows.

Updated on June 05, 2022

Comments

  • prabhakaran
    prabhakaran almost 2 years

    As you may see from my other questions many of you may already got the answer for this. Can you please share that knowledge to me?

  • Fred Nurk
    Fred Nurk about 13 years
    @prabhakaran: Link to what? The standard or drafts? stackoverflow.com/questions/81656/…
  • Fred Nurk
    Fred Nurk about 13 years
    The definitive statement for the type of decimal constants is in C++03 §2.13.1p2, which says either int or long int here.
  • paxdiablo
    paxdiablo about 13 years
    Yes, but it's not necessarily a decimal constant, which is why I was reticent to make a pronouncement. It seems to me that a conforming implementation could convert __LINE__ into 42U and still be kosher. I don't have C++03 handy but C++0x where I sourced my stuff from states "integer constant" and that includes U, L, UL, LL and ULL types.
  • Fred Nurk
    Fred Nurk about 13 years
    C++03 exactly says "a decimal constant".
  • paxdiablo
    paxdiablo about 13 years
    @Fred: don't get me wrong, I'm sure you're right and it translates to the banal 42 - it's just my sources were unclear on the matter :-)
  • MSalters
    MSalters about 13 years
    @prabhakaran: HRESULT is a typedef, not a seperate and distinct type. In particular, it's a typedef for a numerical type. You can convert 37 to a HRESULT. Now, that 37 could also occur because you had a __LINE__ macro on line 37.
  • Steve Jessop
    Steve Jessop about 13 years
    @Fred: Although "decimal constant" is defined in C++03 only indirectly, by non-normative footnote 21, which refers to C89. In C++ 42 is a decimal-literal, and 42U is not a decimal-literal, it's an integer-literal. In C 42 is a decimal constant, and 42U isn't. So the intent is clear, but paxdiablo is right that they messed up the wording in C++03. 16.8/1 should have said "decimal literal", I think, but they've copied C without changing it. If in C++0x they've changed it to "integer constant", that's interesting because presumably that can be 42U, a breaking change.
  • prabhakaran
    prabhakaran about 13 years
    As you said the LINE macro's type is int. But due to the fact that I had a over loaded function which took long as its argument, my int receiving overloaded function can't get any chance. I forgot the sequence of operator overloading argument match. Now I corrected the functions. Thank you for your reply
  • prabhakaran
    prabhakaran about 13 years
    @MSalters Yes,you are exactly correct, thank you for your correction. My above comment tells the fact exactly.
  • Fred Nurk
    Fred Nurk about 13 years
    @Steve: Don't forget C is a normative reference for C++. But I don't see how what you said changes "but it's not necessarily a decimal constant" => "C++03 exactly says 'a decimal constant'".
  • Steve Jessop
    Steve Jessop about 13 years
    @Fred: it's normative for specific cases where C++ says that what is said in C is true, obvious examples being the contents of most C headers. It is not the case in general that any definition made in C89 applies also to C++. I agree with you that in C++03 it is a decimal constant, I'm just pointing out that C++ only very tentatively defines "decimal constant" at all, in a comment stating an intention by comparison with C. I don't think they meant to use the phrase "decimal constant" in C++ at all, but they did and so we must deduce whatever meaning for it we can scrape together.
  • Nathan Osman
    Nathan Osman about 11 years
    What if I have a source file with 2,147,483,647 lines? (Okay, I'm kidding :P)
  • Fred Nurk
    Fred Nurk over 10 years
    @NathanOsman: You'd need one more, to make 2,147,483,648. However, I'm more worried about #line directive abuse, but more != greatly.