Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. auto w = unsigned long{23};
Forum Updated to NodeBB v4.3 + New Features

auto w = unsigned long{23};

Scheduled Pinned Locked Moved Unsolved C++ Gurus
c++11
4 Posts 3 Posters 2.0k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    Hi! I got a C++11 newbie question. The following works as expected ...

    auto w = long{23};
    

    ... but if I try this ...

    auto w = unsigned long{23};
    

    ... gcc says ...

    expected primary-expression before 'unsigned'

    What's the correct C++11'ish way to solve this?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Sounds like a bug in gcc. Works ok in MSVC. There are couple of ways to fix it:

      A (non-standard, borrowed from C) workaround that seems to work on gcc (but correctly fails in MSVC) is:

      auto w = (unsigned long){23};
      

      But don't use that. Seriously :) The standard compliant workaround is a using declaration (or a typedef if you prefer):

      using ulong = unsigned long;
      auto w = ulong{23};
      

      But honestly, for simple types just do this:

      auto w = 23ul;
      

      or this if you really want uniform syntax:

      auto w{23ul};
      

      EDIT: Some people claim that it's in fact MSVC that is in error and it's not a correct syntax. You'd better look it up in the standard if you want to know for sure. To me it's too obscure to bother :P

      ? 1 Reply Last reply
      4
      • Chris KawaC Chris Kawa

        Sounds like a bug in gcc. Works ok in MSVC. There are couple of ways to fix it:

        A (non-standard, borrowed from C) workaround that seems to work on gcc (but correctly fails in MSVC) is:

        auto w = (unsigned long){23};
        

        But don't use that. Seriously :) The standard compliant workaround is a using declaration (or a typedef if you prefer):

        using ulong = unsigned long;
        auto w = ulong{23};
        

        But honestly, for simple types just do this:

        auto w = 23ul;
        

        or this if you really want uniform syntax:

        auto w{23ul};
        

        EDIT: Some people claim that it's in fact MSVC that is in error and it's not a correct syntax. You'd better look it up in the standard if you want to know for sure. To me it's too obscure to bother :P

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

        @Chris-Kawa Thank you for your reply and for testing it with MSVC. Interesting you think this might be a gcc bug; I always refrain from blaming the compiler because when something went wrong in the past in the end it was all my fault every single time. :) Also thanks for your workaround suggestions. The using-workaround is what I use by now.

        Edit: Thanks for the link. Yes, it's obscure.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          gcc's not alone. My old OS X clang isn't happy either with it.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved