接続数

COUNTER379099FROM 2012/5/16

MISRA-C diary(C言語日誌)

MISRA-C >> Article details

2014/06/12

C++N3242, 2011 (7) 2.14 Literals 2.14.8 User-defined literals

Tweet ThisSend to Facebook | by kaizen
// 1 filename:cpp2011-2-14-8.cpp
// ver 0.1 June.12, 2014
//
// 2 original examples and/or notes:
// (c) ISO/IEC JTC1 SC22 WG21 N3242, April 12, 2011
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf
// 2 Lexical conventions 2.14 Literals 2.14.8 User-defined literals
//
// 3 compile and output mechanism:
// (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp,
//
// 4 compile errors and/or wornings:
// 4.1(c) Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
// Target: x86_64-apple-darwin13.2.0,  Thread model: posix
// Command/Options: c++ -std=c++11 -stdlib=libc++ -Wall cpp2011-2-14-8.cpp 
// (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign.

// 4.2. g++-4.9 (GCC) 4.9.0 20131229 (experimental)
// Copyright (C) 2013 Free Software Foundation, Inc.
// This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++-4.9  -std=c++11  -Wall cpp2011-2-14-8.cpp 
// g++-4.9: error: unrecognized command line option '-stdlib=libc++'
// Configuration:brew install gcc49
//
// 4.3. Visual Studio Express 2013, 
// (c) Microsoft http://www.visualstudio.com/
// SPEC:
// Windows 7, .NET Framework
// (c) VMware, Inc.
// VMWare fusion 6
//
// 5. Hardware:  MacBook Pro, 
//(c) Intel http://ark.intel.com/products/37006/
//Core 2 Duo 2.53GHz, 8GB, 1067MHz DDR3
//
// 6. Special Thanks: Upper organizatios and 
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// Japan Standard Association, http://bit.ly/1lzykg1
// NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html
// Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html
// WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html
// SevenWise.co., http://www.7ws.co.jp/index.html
// TOYOTA Motor Corporation, http://toyota.jp/
// IT planning Inc., http://www.itpl.co.jp/en/index.html
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki co. Ltd., http://www.aisin.com/
// Spancion Inc., http://www.spansion.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC:Open Source Conference, http://www.ospn.jp/

#include <iostream>
//#include <string.h>
//#include <cassert>

using namespace std;

//const char x[] ="x";cpp2011-2-14-8.cpp:72:9: error: invalid suffix on literal; C++11 requires a space between literal and identifier [-Wreserved-user-defined-literal]
#define x = "x"

long double operator "" w(long double);
std::string operator "" w(const char16_t*, size_t);
unsigned operator "" w(const char*);

int main() {
1.2w; // calls operator "" w(1.2L)
u"one"w; // calls operator "" w(u"one", 3)
12w; // calls operator "" w("12")
//"two"w; // error: no applicable literal operator
//cpp2011-2-14-8.cpp:76:13: error: read-only variable is not assignable
L"A" "B" "C"x ; // OK: same as L"ABC"x
//"P"x "Q" "R"y; // error: two different ud-suffixes

return 0;
}
// 1. error 
// 1.1 LLVM  -std=c++11


// 1.2 gcc4.9 -std=c++11

// 2. output -std=c++03

llvm, gcc, -std=c++11, c++03のいずれでもまだコンパイルエラーが取れてない。次に進んでから戻る予定。

20180424追記
```

#define msg "C++N3242 2 Lexical conventions 2.14 Literals 2.14.8 User-defined literals"


#include <iostream>

#include <cassert>

#include <string>


using namespace std;


long double operator "" w(long double);

std::string operator "" w(const char16_t*, size_t);

unsigned operator "" w(const char*);


int main() {

1.2w; // calls operator "" w(1.2L)

u"one"w; // calls operator "" w(u"one", 3)

12w; // calls operator "" w("12")

//"two"w; // error: no applicable literal operator

//cpp2011-2-14-8.cpp:76:13: error: read-only variable is not assignable



cout << msg << std::endl;

return EXIT_SUCCESS;;

}

```

$ ./cppall.sh cpp2011-2-14-8a

$ clang++ cpp2011-2-14-8a.cpp -std=c++03 -Wall

cpp2011-2-14-8a.cpp:63:22: error: expected a type

long double operator "" w(long double);

                     ^

cpp2011-2-14-8a.cpp:64:22: error: expected a type

std::string operator "" w(const char16_t*, size_t);

                     ^

cpp2011-2-14-8a.cpp:65:19: error: expected a type

unsigned operator "" w(const char*);

                  ^

cpp2011-2-14-8a.cpp:68:4: error: invalid suffix 'w' on floating constant

1.2w; // calls operator "" w(1.2L)

   ^

cpp2011-2-14-8a.cpp:69:1: error: use of undeclared identifier 'u'

u"one"w; // calls operator "" w(u"one", 3)

^

cpp2011-2-14-8a.cpp:70:3: error: invalid suffix 'w' on integer constant

12w; // calls operator "" w("12")

  ^

6 errors generated.

$ clang++ cpp2011-2-14-8a.cpp -std=c++11 -Wall

cpp2011-2-14-8a.cpp:63:13: warning: user-defined literal suffixes not starting with '_' are

      reserved; no literal will invoke this operator [-Wuser-defined-literals]

long double operator "" w(long double);

            ^

cpp2011-2-14-8a.cpp:64:13: warning: user-defined literal suffixes not starting with '_' are

      reserved; no literal will invoke this operator [-Wuser-defined-literals]

std::string operator "" w(const char16_t*, size_t);

            ^

cpp2011-2-14-8a.cpp:65:10: warning: user-defined literal suffixes not starting with '_' are

      reserved; no literal will invoke this operator [-Wuser-defined-literals]

unsigned operator "" w(const char*);

         ^

cpp2011-2-14-8a.cpp:68:4: error: invalid suffix 'w' on floating constant

1.2w; // calls operator "" w(1.2L)

   ^

cpp2011-2-14-8a.cpp:69:7: error: invalid suffix on literal; C++11 requires a space between

      literal and identifier [-Wreserved-user-defined-literal]

u"one"w; // calls operator "" w(u"one", 3)

      ^

       

cpp2011-2-14-8a.cpp:69:7: error: expected ';' after expression

u"one"w; // calls operator "" w(u"one", 3)

      ^

      ;

cpp2011-2-14-8a.cpp:69:7: error: use of undeclared identifier 'w'

cpp2011-2-14-8a.cpp:70:3: error: invalid suffix 'w' on integer constant

12w; // calls operator "" w("12")

  ^

cpp2011-2-14-8a.cpp:69:1: warning: expression result unused [-Wunused-value]

u"one"w; // calls operator "" w(u"one", 3)

^~~~~~

4 warnings and 5 errors generated.

$ clang++ cpp2011-2-14-8a.cpp -std=c++17 -Wall

cpp2011-2-14-8a.cpp:63:13: warning: user-defined literal suffixes not starting with '_' are

      reserved; no literal will invoke this operator [-Wuser-defined-literals]

long double operator "" w(long double);

            ^

cpp2011-2-14-8a.cpp:64:13: warning: user-defined literal suffixes not starting with '_' are

      reserved; no literal will invoke this operator [-Wuser-defined-literals]

std::string operator "" w(const char16_t*, size_t);

            ^

cpp2011-2-14-8a.cpp:65:10: warning: user-defined literal suffixes not starting with '_' are

      reserved; no literal will invoke this operator [-Wuser-defined-literals]

unsigned operator "" w(const char*);

         ^

cpp2011-2-14-8a.cpp:68:4: error: invalid suffix 'w' on floating constant

1.2w; // calls operator "" w(1.2L)

   ^

cpp2011-2-14-8a.cpp:69:7: error: invalid suffix on literal; C++11 requires a space between

      literal and identifier [-Wreserved-user-defined-literal]

u"one"w; // calls operator "" w(u"one", 3)

      ^

       

cpp2011-2-14-8a.cpp:69:7: error: expected ';' after expression

u"one"w; // calls operator "" w(u"one", 3)

      ^

      ;

cpp2011-2-14-8a.cpp:69:7: error: use of undeclared identifier 'w'

cpp2011-2-14-8a.cpp:70:3: error: invalid suffix 'w' on integer constant

12w; // calls operator "" w("12")

  ^

cpp2011-2-14-8a.cpp:69:1: warning: expression result unused [-Wunused-value]

u"one"w; // calls operator "" w(u"one", 3)

^~~~~~

4 warnings and 5 errors generated.


$ g++-7 cpp2011-2-14-8a.cpp -std=c++03  -Wall

cpp2011-2-14-8a.cpp:70:1:error: invalid suffix "w" on integer constant

 12w; // calls operator "" w("12")

 ^~~

cpp2011-2-14-8a.cpp:63:13:warning: user-defined literals only available with -std=c++11 or -std=gnu++11

 long double operator "" w(long double);

             ^~~~~~~~

cpp2011-2-14-8a.cpp:63:13:warning: user-defined literals only available with -std=c++11 or -std=gnu++11

cpp2011-2-14-8a.cpp:63:25:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 long double operator "" w(long double);

                         ^

cpp2011-2-14-8a.cpp:63:38:warning: floating point suffix 'w' shadowed by implementation

 long double operator "" w(long double);

                                      ^

cpp2011-2-14-8a.cpp:64:13:warning: user-defined literals only available with -std=c++11 or -std=gnu++11

 std::string operator "" w(const char16_t*, size_t);

             ^~~~~~~~

cpp2011-2-14-8a.cpp:64:13:warning: user-defined literals only available with -std=c++11 or -std=gnu++11

cpp2011-2-14-8a.cpp:64:25:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 std::string operator "" w(const char16_t*, size_t);

                         ^

cpp2011-2-14-8a.cpp:64:33:error: 'char16_t' does not name a type; did you mean 'wchar_t'?

 std::string operator "" w(const char16_t*, size_t);

                                 ^~~~~~~~

                                 wchar_t

cpp2011-2-14-8a.cpp:64:50:error: 'std::__cxx11::string operator""w(const int*, size_t)' has invalid argument list

 std::string operator "" w(const char16_t*, size_t);

                                                  ^

cpp2011-2-14-8a.cpp:65:10:warning: user-defined literals only available with -std=c++11 or -std=gnu++11

 unsigned operator "" w(const char*);

          ^~~~~~~~

cpp2011-2-14-8a.cpp:65:10:warning: user-defined literals only available with -std=c++11 or -std=gnu++11

cpp2011-2-14-8a.cpp:65:22:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 unsigned operator "" w(const char*);

                      ^

cpp2011-2-14-8a.cpp: In function 'int main()':

cpp2011-2-14-8a.cpp:68:5:warning: statement has no effect [-Wunused-value]

 1.2w; // calls operator "" w(1.2L)

     ^

cpp2011-2-14-8a.cpp:69:1:error: 'u' was not declared in this scope

 u"one"w; // calls operator "" w(u"one", 3)

 ^


$ g++-7 cpp2011-2-14-8a.cpp -std=c++11  -Wall

cpp2011-2-14-8a.cpp:63:25:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 long double operator "" w(long double);

                         ^

cpp2011-2-14-8a.cpp:64:25:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 std::string operator "" w(const char16_t*, size_t);

                         ^

cpp2011-2-14-8a.cpp:65:22:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 unsigned operator "" w(const char*);

                      ^

Undefined symbols for architecture x86_64:

  "operator"" w[abi:cxx11](char16_t const*, unsigned long)", referenced from:

      _main in ccMTFAjX.o

  "operator"" w(char const*)", referenced from:

      _main in ccMTFAjX.o

  "operator"" w(long double)", referenced from:

      _main in ccMTFAjX.o

ld: symbol(s) not found for architecture x86_64

collect2: error: ld returned 1 exit status


$ g++-7 cpp2011-2-14-8a.cpp -std=c++17  -Wall

cpp2011-2-14-8a.cpp:63:25:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 long double operator "" w(long double);

                         ^

cpp2011-2-14-8a.cpp:64:25:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 std::string operator "" w(const char16_t*, size_t);

                         ^

cpp2011-2-14-8a.cpp:65:22:warning: literal operator suffixes not preceded by '_' are reserved for future standardization [-Wliteral-suffix]

 unsigned operator "" w(const char*);

                      ^

Undefined symbols for architecture x86_64:

  "operator"" w[abi:cxx11](char16_t const*, unsigned long)", referenced from:

      _main in cc9bt5dZ.o

  "operator"" w(char const*)", referenced from:

      _main in cc9bt5dZ.o

  "operator"" w(long double)", referenced from:

      _main in cc9bt5dZ.o

ld: symbol(s) not found for architecture x86_64

collect2: error: ld returned 1 exit status

---
clang++コンパイルエラー、g++リンクエラー

----

#include <iostream>

#include <cassert>

#include <string>


using namespace std;


int main() {

L"A" "B" "C"x ; // OK: same as L"ABC"x

//"P"x "Q" "R"y; // error: two different ud-suffixes


return 0;

}

---

$ ./cppall.sh cpp2011-2-14-8b

$ clang++ cpp2011-2-14-8b.cpp -std=c++03 -Wall

cpp2011-2-14-8b.cpp:64:13: error: expected ';' after expression

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

            ;

cpp2011-2-14-8b.cpp:64:13: error: use of undeclared identifier 'x'

cpp2011-2-14-8b.cpp:64:1: warning: expression result unused [-Wunused-value]

L"A" "B" "C"x ; // OK: same as L"ABC"x

^~~~~~~~~~~~

1 warning and 2 errors generated.

$ clang++ cpp2011-2-14-8b.cpp -std=c++11 -Wall

cpp2011-2-14-8b.cpp:64:13: error: invalid suffix on literal; C++11 requires a space between

      literal and identifier [-Wreserved-user-defined-literal]

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

             

cpp2011-2-14-8b.cpp:64:13: error: expected ';' after expression

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

            ;

cpp2011-2-14-8b.cpp:64:13: error: use of undeclared identifier 'x'

cpp2011-2-14-8b.cpp:64:1: warning: expression result unused [-Wunused-value]

L"A" "B" "C"x ; // OK: same as L"ABC"x

^~~~~~~~~~~~

1 warning and 3 errors generated.

$ clang++ cpp2011-2-14-8b.cpp -std=c++17 -Wall

cpp2011-2-14-8b.cpp:64:13: error: invalid suffix on literal; C++11 requires a space between

      literal and identifier [-Wreserved-user-defined-literal]

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

             

cpp2011-2-14-8b.cpp:64:13: error: expected ';' after expression

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

            ;

cpp2011-2-14-8b.cpp:64:13: error: use of undeclared identifier 'x'

cpp2011-2-14-8b.cpp:64:1: warning: expression result unused [-Wunused-value]

L"A" "B" "C"x ; // OK: same as L"ABC"x

^~~~~~~~~~~~

1 warning and 3 errors generated.


$ g++-7 cpp2011-2-14-8b.cpp -std=c++03  -Wall

cpp2011-2-14-8b.cpp: In function 'int main()':

cpp2011-2-14-8b.cpp:64:13:error: expected ';' before 'x'

 L"A" "B" "C"x ; // OK: same as L"ABC"x

             ^

cpp2011-2-14-8b.cpp:64:15:warning: statement has no effect [-Wunused-value]

 L"A" "B" "C"x ; // OK: same as L"ABC"x

               ^


$ g++-7 cpp2011-2-14-8b.cpp -std=c++11  -Wall

cpp2011-2-14-8b.cpp: In function 'int main()':

cpp2011-2-14-8b.cpp:64:10:error: unable to find string literal operator 'operator""x' with 'const wchar_t [4]', 'long unsigned int' arguments

 L"A" "B" "C"x ; // OK: same as L"ABC"x

          ^~~~


$ g++-7 cpp2011-2-14-8b.cpp -std=c++17  -Wall

cpp2011-2-14-8b.cpp: In function 'int main()':

cpp2011-2-14-8b.cpp:64:10:error: unable to find string literal operator 'operator""x' with 'const wchar_t [4]', 'long unsigned int' arguments

 L"A" "B" "C"x ; // OK: same as L"ABC"x

          ^~~~

OgawaKiyoshi-no-MacBook-Pro:cpp2011 ogawakiyoshi$ vi cpp2011-2-14-8b.cpp

OgawaKiyoshi-no-MacBook-Pro:cpp2011 ogawakiyoshi$ ./cppall.sh cpp2011-2-14-8b

$ clang++ cpp2011-2-14-8b.cpp -std=c++03 -Wall

cpp2011-2-14-8b.cpp:64:13: error: expected ';' after expression

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

            ;

cpp2011-2-14-8b.cpp:64:13: error: use of undeclared identifier 'x'

cpp2011-2-14-8b.cpp:64:1: warning: expression result unused [-Wunused-value]

L"A" "B" "C"x ; // OK: same as L"ABC"x

^~~~~~~~~~~~

1 warning and 2 errors generated.

$ clang++ cpp2011-2-14-8b.cpp -std=c++11 -Wall

cpp2011-2-14-8b.cpp:64:13: error: invalid suffix on literal; C++11 requires a space between

      literal and identifier [-Wreserved-user-defined-literal]

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

             

cpp2011-2-14-8b.cpp:64:13: error: expected ';' after expression

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

            ;

cpp2011-2-14-8b.cpp:64:13: error: use of undeclared identifier 'x'

cpp2011-2-14-8b.cpp:64:1: warning: expression result unused [-Wunused-value]

L"A" "B" "C"x ; // OK: same as L"ABC"x

^~~~~~~~~~~~

1 warning and 3 errors generated.

$ clang++ cpp2011-2-14-8b.cpp -std=c++17 -Wall

cpp2011-2-14-8b.cpp:64:13: error: invalid suffix on literal; C++11 requires a space between

      literal and identifier [-Wreserved-user-defined-literal]

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

             

cpp2011-2-14-8b.cpp:64:13: error: expected ';' after expression

L"A" "B" "C"x ; // OK: same as L"ABC"x

            ^

            ;

cpp2011-2-14-8b.cpp:64:13: error: use of undeclared identifier 'x'

cpp2011-2-14-8b.cpp:64:1: warning: expression result unused [-Wunused-value]

L"A" "B" "C"x ; // OK: same as L"ABC"x

^~~~~~~~~~~~

1 warning and 3 errors generated.


$ g++-7 cpp2011-2-14-8b.cpp -std=c++03  -Wall

cpp2011-2-14-8b.cpp: In function 'int main()':

cpp2011-2-14-8b.cpp:64:13:error: expected ';' before 'x'

 L"A" "B" "C"x ; // OK: same as L"ABC"x

             ^

cpp2011-2-14-8b.cpp:64:15:warning: statement has no effect [-Wunused-value]

 L"A" "B" "C"x ; // OK: same as L"ABC"x

               ^


$ g++-7 cpp2011-2-14-8b.cpp -std=c++11  -Wall

cpp2011-2-14-8b.cpp: In function 'int main()':

cpp2011-2-14-8b.cpp:64:10:error: unable to find string literal operator 'operator""x' with 'const wchar_t [4]', 'long unsigned int' arguments

 L"A" "B" "C"x ; // OK: same as L"ABC"x

          ^~~~


$ g++-7 cpp2011-2-14-8b.cpp -std=c++17  -Wall

cpp2011-2-14-8b.cpp: In function 'int main()':

cpp2011-2-14-8b.cpp:64:10:error: unable to find string literal operator 'operator""x' with 'const wchar_t [4]', 'long unsigned int' arguments

 L"A" "B" "C"x ; // OK: same as L"ABC"x

          ^~~~


---

<この稿は書きかけです。順次追記しています。>
twitter:@kaizen_nagoya
08:43 | Impressed! | Voted(0) | Comment(0)