diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2013-02-05 23:08:08 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2013-02-08 06:36:31 +0000 |
commit | c1b99d427987524349e0c9ae427b7c694231d266 (patch) | |
tree | 0e410d95b27a93c3246fc229c0a87dc6b9c05bf7 | |
parent | dbf4821b18e0811564ae221619efaee3a3fd5ba2 (diff) | |
download | swig-c1b99d427987524349e0c9ae427b7c694231d266.tar.gz |
User defined literals: fix for %rename and update docs
-rw-r--r-- | Doc/Manual/Cpp0x.html | 28 | ||||
-rw-r--r-- | Examples/test-suite/cpp0x_userdefined_literals.i | 27 | ||||
-rw-r--r-- | Source/CParse/parser.y | 3 |
3 files changed, 53 insertions, 5 deletions
diff --git a/Doc/Manual/Cpp0x.html b/Doc/Manual/Cpp0x.html index b85df1b09..e9576ec84 100644 --- a/Doc/Manual/Cpp0x.html +++ b/Doc/Manual/Cpp0x.html @@ -631,7 +631,7 @@ In the above example <tt>SIZE</tt> is of course wrapped as a constant. <H3><a name="Cpp0x_New_string_literals"></a>7.2.18 New string literals</H3> -<p>SWIG fully supports unicode string constants and raw string literals.</p> +<p>SWIG supports unicode string constants and raw string literals.</p> <div class="code"><pre> // New string literals @@ -685,12 +685,32 @@ OutputType operator "" _mySuffix(const char32_t * string_values, size_t num_char </pre></div> <p> -Note that the %rename directive currently does not parse the double quotes, so these can't be easily accessed from -target languages. +Like other operators that SWIG parses, a warning is given about renaming the operator in order for it to be wrapped: </p> +<div class="shell"><pre> +example.i:27: Warning 503: Can't wrap 'operator "" _myRawLiteral' unless renamed to a valid identifier. +</pre></div> + +<p> +If %rename is used, then it can be called like any other wrapped method. +Currently you need to specify the full declaration including parameters for %rename: +</p> + +<div class="code"><pre> +%rename(MyRawLiteral) operator"" _myRawLiteral(const char * value); +</pre></div> + +<p> +Or if you just wish to ignore it altogether: +</p> + +<div class="code"><pre> +%ignore operator "" _myRawLiteral(const char * value); +</pre></div> + <p> -Use of user-defined literals such as the following still give a syntax error: +Note that use of user-defined literals such as the following still give a syntax error: </p> <div class="code"><pre> diff --git a/Examples/test-suite/cpp0x_userdefined_literals.i b/Examples/test-suite/cpp0x_userdefined_literals.i index b21ff762d..d20345fed 100644 --- a/Examples/test-suite/cpp0x_userdefined_literals.i +++ b/Examples/test-suite/cpp0x_userdefined_literals.i @@ -2,6 +2,17 @@ introduced in C++0x. */ %module cpp0x_userdefined_literals +// Unfortunately full declaration is needed for %rename atm, the parameter list cannot be omitted. +%rename(MyRawLiteral) operator"" _myRawLiteral(const char * value); +%rename(MySuffixIntegral) operator "" _mySuffixIntegral(unsigned long long); +%rename(MySuffixFloat) operator "" _mySuffixFloat(long double); +%rename(MySuffix1) operator "" _mySuffix1(const char * string_values, size_t num_chars); +%rename(MySuffix2) operator "" _mySuffix2(const wchar_t * string_values, size_t num_chars); +%rename(MySuffix3) operator "" _mySuffix3(const char16_t * string_values, size_t num_chars); +%rename(MySuffix4) operator "" _mySuffix4(const char32_t * string_values, size_t num_chars); + +%ignore operator "" _myRawLiteralIgnored(const char * value); + %inline %{ #include <iostream> @@ -23,4 +34,20 @@ OutputType operator "" _mySuffix2(const wchar_t * string_values, size_t num_char OutputType operator "" _mySuffix3(const char16_t * string_values, size_t num_chars) { return OutputType(300); } OutputType operator "" _mySuffix4(const char32_t * string_values, size_t num_chars) { return OutputType(400); } +OutputType operator"" _myRawLiteralIgnored(const char * value) { return OutputType(15); } %} + +%{ +// TODO: SWIG cannot parse these +OutputType some_variable_a = 1234_myRawLiteral; + +OutputType some_variable_b = 1234_mySuffixIntegral; +OutputType some_variable_c = 3.1416_mySuffixFloat; + +OutputType some_variable_d = "1234"_mySuffix1; +OutputType some_variable_e = u8"1234"_mySuffix1; +OutputType some_variable_f = L"1234"_mySuffix2; +OutputType some_variable_g = u"1234"_mySuffix3; +OutputType some_variable_h = U"1234"_mySuffix4; +%} + diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index f1973a05c..dfa2f8de2 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -5677,10 +5677,11 @@ direct_declarator : idcolon { } } /* User-defined string literals. eg. - int operator""_mySuffix(const char* val, int length) {...} */ + int operator"" _mySuffix(const char* val, int length) {...} */ /* This produces one S/R conflict. */ | OPERATOR ID LPAREN parms RPAREN { SwigType *t; + Append($1, " "); /* intervening space is mandatory */ Append($1, Char($2)); $$.id = Char($1); t = NewStringEmpty(); |