Menu

Error Invalid ## usage expanding macro when token pasting string constant to user defined literal

2019-05-08
2019-05-13
  • Bryn Edwards

    Bryn Edwards - 2019-05-08

    Simple example:

    #define NUM_CHAR(str) str##_numch
    
    inline auto operator "" _numch(const char *ch, std::size_t size) noexcept -> std:size_t
    {
      return size;
    }
    
    int main(void)
    {
      auto x = NUM_CHAR("abc");
    }
    

    Produces on Cppcheck 1.87 :
    (error) failed to expand 'NUM_CHAR', Invalid ## usage when expanding 'NUM_CHAR'.

    Expected behaviour is that this does not produce an error.

    Running Cppcheck on the code with macro expanded does not produce any error:

    inline auto operator "" _numch(const char *ch, std::size_t size) noexcept -> std:size_t
    {
      return size;
    }
    
    int main(void)
    {
      auto x = "abc"_numch;
    }
    

    Note that this only occurs for pasting string constants, the following does not produce any error:

    #define HALF(num) num##_half
    
    inline auto operator "" _half(long double num) noexcept -> long double
    {
      return 0.5 * num;
    }
    
    int main(void)
    {
      auto x = HALF(100.0);
    }
    
     
  • Bryn Edwards

    Bryn Edwards - 2019-05-11

    Looked into this briefly.
    Appears to be an issue with simplecpp, rather than cppcheck.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.