From 61061ff150208f28c3e95bcd0f79794a2e15a1c3 Mon Sep 17 00:00:00 2001 From: g Date: Wed, 26 Oct 2016 23:55:04 -0700 Subject: [PATCH 1/2] Added description of the operator[] caveats --- Doc/Manual/SWIGPlus.html | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/Manual/SWIGPlus.html b/Doc/Manual/SWIGPlus.html index 74d9c9083..e1bc49f9e 100644 --- a/Doc/Manual/SWIGPlus.html +++ b/Doc/Manual/SWIGPlus.html @@ -2766,6 +2766,16 @@ have to handle it like a normal function. For example: are ignored as well as conversion operators.

+
  • The index operator, operator[], is particularly difficult to overload due to the C++ +implementation. Specifically, the get and set operators in other languages typically are separated +into two methods such that additional logic can be packed into the operations; C# uses +this[type key] { get { ... } set { ... }}, Python uses +__getitem__ and __setitem__, etc. In C++, the setter only exists if the return +type of operator[] is a reference, and if the creator of the method wanted unique logic when +getting or setting a value, they need to use a +temporary proxy. +
  • +
  • The semantics of certain C++ operators may not match those in the target language.
  • From 0886fc6fe62a66177258b38d305e3b23f8b7f117 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Sun, 30 Oct 2016 14:52:35 +0000 Subject: [PATCH 2/2] Edit operator[] additions --- Doc/Manual/SWIGPlus.html | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Doc/Manual/SWIGPlus.html b/Doc/Manual/SWIGPlus.html index e1bc49f9e..95a3d054b 100644 --- a/Doc/Manual/SWIGPlus.html +++ b/Doc/Manual/SWIGPlus.html @@ -2763,17 +2763,27 @@ have to handle it like a normal function. For example:
  • Certain operators are ignored by default. For instance, new and delete operators -are ignored as well as conversion operators. -

  • +are ignored as well as conversion and index operators. A warning such as the one below is shown: +

    -
  • The index operator, operator[], is particularly difficult to overload due to the C++ -implementation. Specifically, the get and set operators in other languages typically are separated +

    +

    +
    +example.i:12: Warning 503: Can't wrap 'operator []' unless renamed to a valid identifier.
    +
    +
    +
  • + +
  • The index operator, operator[], is particularly difficult to overload due to differences in C++ +implementations. Specifically, the get and set operators in other languages typically are separated into two methods such that additional logic can be packed into the operations; C# uses this[type key] { get { ... } set { ... }}, Python uses -__getitem__ and __setitem__, etc. In C++, the setter only exists if the return -type of operator[] is a reference, and if the creator of the method wanted unique logic when -getting or setting a value, they need to use a -temporary proxy. +__getitem__ and __setitem__, etc. In C++ if the return +type of operator[] is a reference and the method is const, it is often indicative of the setter, +and and the getter is usually a const function return an object by value. +In the absence of any hard and fast rules and the fact that there may be multiple index operators, +it is up to the user to choose the getter and setter to use by using %rename as shown earlier. +

  • The semantics of certain C++ operators may not match those in the target language.