Thread: Possible GCC Bug in User-Defined Floating Point Literal Operator

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    Possible GCC Bug in User-Defined Floating Point Literal Operator

    I may have found a bug in GCC 5.4.0 and 6.1.1.

    Consider the following program:

    Code:
    class foo
    {
      public:
        void Bar() {}
    };
    
    foo operator""_FOO(long double ld)
    {
      return foo();
    }
    
    foo operator""_FOO(unsigned long long int)
    {
      return foo();
    }
    
    int main()
    {
      3.14_FOO.Bar();
      1234_FOO.Bar();
      return 0;
    }
    I receive the following error.

    Code:
    main.cpp: In function ‘int main()’:
    main.cpp:19:3: error: unable to find numeric literal operator ‘operator""_FOO.Bar’
       3.14_FOO.Bar();
       ^
    main.cpp:20:3: error: unable to find numeric literal operator ‘operator""_FOO.Bar’
       1234_FOO.Bar();
       ^
    It appears to be treating the '.Bar' portion as if it were part of the literal operator. If I add a space after the literal operator, it compiles without errors.

    The only active GCC bug I can find that seems related is this one: botched floating-point UDL

    Is this correct behavior, or have I discovered a bug?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,652
    Same result with clang.
    You can put more dots there without any change!
    Code:
    3.14_FOO.....Bar()
    I think it is a bug, the literal is supposed to be an identifier or a string literal itself, afaik.
    If not, what is a good rationale for allowing dots there?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I haven't tested, but I take it it's a precedence issue. What happens if you add parans?
    (3.14_FOO).Bar();
    (1234_FOO).Bar();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The problem is that it seems to be accepting dots as part of the identifier of the udl. It even accepts dollar signs. But you can't put dots or dollar signs in the literal operator identifier.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,094
    Is this correct behavior, or have I discovered a bug?
    O_o

    You've stumbled upon one of the issues with the standard.

    You can think of the issue like the `>>` token in the fragment `std::vector<std::vector<int>>` as interpreted by the earlier standards.

    If not, what is a good rationale for allowing dots there?
    The dots aren't allowed. You are learning compiler theory so you should take a look at the rules when you have the time.

    Code:
    struct S{void doSomething(){}};
    
    S operator "" _s (const char *, unsigned long){return(S());}
    
    int main()
    {
         ""_s.doSomething(); // Hint
         return(0);
    }
    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    So it is a bug, but not necessarily with the compiler itself.

    Interestingly enough, VC++ 2015 compiles this code with no problems.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Even more interestingly, if I define a literal operator _FOO for strings, it works fine, even with the dot directly adjacent to the _FOO.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    So it is a bug, but not necessarily with the compiler itself.

    Interestingly enough, VC++ 2015 compiles this code with no problems.
    Most likely because Microsoft's compiler parses code entirely different from GCC & Clang because it's using an outdated token parsing approach, thanks to the compiler being invented so many years ago. The team is working on modernizing that, so in the future, it might just spit out the same error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined literal
    By cstryx in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2015, 05:49 PM
  2. create user define literal?
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 07-14-2014, 03:51 AM
  3. Replies: 3
    Last Post: 04-14-2012, 09:14 AM
  4. user defined operator returns another type?
    By TriKri in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2007, 02:47 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM

Tags for this Thread