Thread: User defined literal

  1. #1
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123

    User defined literal

    Should something like this really work?
    Code:
    template <char...> double operator "" _\u03C0();
    Since \u03C0 is π?

    I get an error in MinGW that complains about a stray '\' in my source, yet there are documents online that say this *should* be okay.

    edit:
    Code:
    mingw32-g++ (GCC) 4.8.1

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't think you can use unicode characters in identifiers in any version of the C++ standard. The '\u' notation only works inside literal char and string values, i.e. in quotes.
    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?

  3. #3
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Maybe I was mislead then, and they wrote it here: User-defined literals (since C++11) - cppreference.com

    As to show that you are meant to substitute the unicode notation with the actual replacement before trying to compile...? Doing so still gives me an error though:
    Code:
    error: stray '\200' in program
    This just throws me off guard if that is the case. I still don't know why you'd ever use such a character in a literal suffix, but I was just curious. I'd probably use something else anyways to avoid using unicode characters as literals.. (Ex: _pi)

    I still don't get as to how they comment this as "OK".
    Last edited by cstryx; 02-16-2015 at 03:29 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    According to this stack overflow question, it's allowed, but if you're using GCC, you have to tell it what encoding you're using. This stack overflow question seems to show the command line options you'll need.
    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?

  5. #5
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    I wrote this in C, but this seems to work:
    Code:
    #include <stdio.h>
    
    // GCC: -fextended-identifiers
    #define assign(ucn, val) ucn = val
    
    int main(void)
    {
      int \u0401;
      assign(\u0401, 4);
      printf("%d\n", \u0401);
    }
    And seems to be compliant from what I've read. I didn't change any of the charset compiler flags or add them to the command line because the default as UTF-8 seems to be fine already. What I needed was commented in the code above. Although I haven't tested it, but those may become relevant when I try to actually use unicode in my source.
    Last edited by cstryx; 02-16-2015 at 05:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create user define literal?
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 07-14-2014, 03:51 AM
  2. User defined seed value
    By blue5t1053 in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 07:22 PM
  3. User defined functions
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 02:53 PM
  4. New to user defined functions
    By Extropian in forum C Programming
    Replies: 4
    Last Post: 08-15-2005, 10:45 PM
  5. User Defined Array
    By jerez_z in forum C Programming
    Replies: 27
    Last Post: 06-03-2004, 11:23 AM