diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2014-03-10 19:19:52 +0000 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2014-03-14 01:57:15 +0000 |
commit | 9a45a09aece8da3540250176b710bbd92c2e1b04 (patch) | |
tree | 4af0eaef1b9ddc4d4105a163c83ca787e60896a8 | |
parent | adc3cfeb571f8dd6d072bbab52eebda0bf612656 (diff) | |
download | swig-9a45a09aece8da3540250176b710bbd92c2e1b04.tar.gz |
C++11 conversion operator example and docs added
-rw-r--r-- | Doc/Manual/CPlusPlus11.html | 21 | ||||
-rw-r--r-- | Examples/test-suite/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 { }; </pre></div> -<p>A workaround is to write these as a series of separated classes containing anonymous enums:</p> +<p>A workaround is to write these as a series of separate classes containing anonymous enums:</p> <div class="code"><pre> class PrintingColors { @@ -557,7 +557,7 @@ std::vector<std::vector<int>> myIntTable; <H3><a name="CPlusPlus11_explicit_conversion_operators"></a>7.2.14 Explicit conversion operators</H3> -<p>SWIG correctly parses the keyword <tt>explicit</tt> both for operators and constructors. +<p>SWIG correctly parses the keyword <tt>explicit</tt> for operators in addition to constructors now. For example:</p> <div class="code"><pre> @@ -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; + } +}; </pre></div> <p> -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 <tt>explicit</tt> need renaming to a valid identifier name in order to make +them available as a normal proxy method. </p> <H3><a name="CPlusPlus11_alias_templates"></a>7.2.15 Alias templates</H3> 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; + } +}; %} |