From e186dc13b7a2c31eeea8e4918504ff85b7244765 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 10 Mar 2014 07:07:39 +0000 Subject: [PATCH] C++11 constexpr variables support added --- Doc/Manual/CPlusPlus11.html | 21 ++++++++++---------- Examples/test-suite/cpp11_constexpr.i | 28 ++++++++++++++++++++++++--- Source/CParse/parser.y | 2 +- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Doc/Manual/CPlusPlus11.html b/Doc/Manual/CPlusPlus11.html index 86a042d78..585a70e63 100644 --- a/Doc/Manual/CPlusPlus11.html +++ b/Doc/Manual/CPlusPlus11.html @@ -107,7 +107,7 @@ For example, ignore the move constructor:

-The plan is to ignore them by default in a future version of SWIG. Note that both normal assignment operators as well as move assignment operators are ignored by default in most target languages with the following warning: +The plan is to ignore move constructors by default in a future version of SWIG. Note that both normal assignment operators as well as move assignment operators are ignored by default in most target languages with the following warning:

@@ -120,20 +120,19 @@ example.i:18: Warning 503: Can't wrap 'operator =' unless renamed to a valid ide

7.2.2 Generalized constant expressions

-

SWIG correctly parses the keyword constexpr, but ignores its functionality. Constant functions cannot be used as constants.

+

SWIG parses and identifies the keyword constexpr, but cannot fully utilise it. +These C++ compile time constants are usable as runtime constants from the target languages. +Below shows example usage for assigning a C++ compile time constant from a compile time constant function: +

-constexpr int myConstFunc() { return 10; }
-const int a = myConstFunc(); // results in error
+constexpr int XXX() { return 10; }
+constexpr int YYY = XXX() + 100;
 
-

Users needs to use values or predefined constants when defining the new constant value:

- -
-#define MY_CONST 10
-constexpr int myConstFunc() { return MY_CONST; }
-const int a = MY_CONST; // ok
-
+

+When either of these is used from a target language, a runtime call is made to obtain the underlying constant. +

7.2.3 Extern template

diff --git a/Examples/test-suite/cpp11_constexpr.i b/Examples/test-suite/cpp11_constexpr.i index 95fe5fa2b..412b8132a 100644 --- a/Examples/test-suite/cpp11_constexpr.i +++ b/Examples/test-suite/cpp11_constexpr.i @@ -4,9 +4,31 @@ %module cpp11_constexpr %inline %{ -class TestClass { -public: - constexpr int func() { return 10; } +constexpr int AAA = 10; +constexpr const int BBB = 20; +constexpr int CCC() { return 30; } +constexpr const int DDD() { return 40; } + +constexpr int XXX() { return 10; } +constexpr int YYY = XXX() + 100; + +struct ConstExpressions { + static constexpr const int JJJ = 100; + static constexpr int KKK = 200; + static const int LLL = 300; + constexpr int MMM() { return 400; } + constexpr const int NNN() { return 500; } }; %} +%{ +int Array10[AAA]; +int Array20[BBB]; +int Array30[CCC()]; +int Array40[DDD()]; +int Array100[ConstExpressions::JJJ]; +int Array200[ConstExpressions::KKK]; +int Array300[ConstExpressions::LLL]; +//int Array400[ConstExpressions::MMM()]; +//int Array500[ConstExpressions::NNN()]; +%} diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y index 1bd351560..0ed438e90 100644 --- a/Source/CParse/parser.y +++ b/Source/CParse/parser.y @@ -412,7 +412,7 @@ static void add_symbols(Node *n) { } else { ty = type; } - if (!SwigType_ismutable(ty)) { + if (!SwigType_ismutable(ty) || (storage && Strstr(storage, "constexpr"))) { SetFlag(n,"hasconsttype"); SetFlag(n,"feature:immutable"); }