From 9a45a09aece8da3540250176b710bbd92c2e1b04 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Mon, 10 Mar 2014 19:19:52 +0000 Subject: [PATCH] C++11 conversion operator example and docs added --- Doc/Manual/CPlusPlus11.html | 21 ++++++++++++------- .../cpp11_explicit_conversion_operators.i | 13 ++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Doc/Manual/CPlusPlus11.html b/Doc/Manual/CPlusPlus11.html index d79c3172e..b32c4fe85 100644 --- a/Doc/Manual/CPlusPlus11.html +++ b/Doc/Manual/CPlusPlus11.html @@ -517,7 +517,7 @@ class Color { }; -

A workaround is to write these as a series of separated classes containing anonymous enums:

+

A workaround is to write these as a series of separate classes containing anonymous enums:

 class PrintingColors {
@@ -557,7 +557,7 @@ std::vector<std::vector<int>> myIntTable;
 

7.2.14 Explicit conversion operators

-

SWIG correctly parses the keyword explicit both for operators and constructors. +

SWIG correctly parses the keyword explicit for operators in addition to constructors now. For example:

@@ -575,19 +575,26 @@ class TestClass {
 public:
   //implicit converting constructor
   TestClass(U const &val) { t=val.u; }
+
   // explicit constructor
   explicit TestClass(V const &val) { t=val.v; }
 
   int t;
 };
+
+struct Testable {
+  // explicit conversion operator
+  explicit operator bool() const {
+    return false;
+  }
+};
 

-The usage of explicit constructors and operators is somehow specific to C++ when assigning the value -of one object to another one of different type or translating one type to another. It requires both operator and function overloading features, -which are not supported by the majority of SWIG target languages. Also the constructors and operators are not particulary useful in any -SWIG target languages, because all use their own facilities (eg. classes Cloneable and Comparable in Java) -to achieve particular copy and compare behaviours. +The effect of explicit constructors and operators has little relevance for the proxy classes as target +languages don't have the same concepts of implicit conversions as C++. +Conversion operators either with or without explicit need renaming to a valid identifier name in order to make +them available as a normal proxy method.

7.2.15 Alias templates

diff --git a/Examples/test-suite/cpp11_explicit_conversion_operators.i b/Examples/test-suite/cpp11_explicit_conversion_operators.i index 5e3ba03df..632355afc 100644 --- a/Examples/test-suite/cpp11_explicit_conversion_operators.i +++ b/Examples/test-suite/cpp11_explicit_conversion_operators.i @@ -3,6 +3,9 @@ */ %module cpp11_explicit_conversion_operators +%warnfilter(SWIGWARN_LANG_IDENTIFIER) Testable::operator bool; +%rename(AsInteger) Testable::operator int; + %inline %{ class U { @@ -24,5 +27,15 @@ public: int t; }; + +struct Testable { + // explicit conversion operator + explicit operator bool() const { + return false; + } + explicit operator int() { + return 42; + } +}; %}