f



Templated Overloaded Operator

{ edited by mod to shorten lines to ~70 characters. -mod }

Hi,

  The following code (http://ideone.com/e.js/6pTF0n) works fine:

#include <iostream>
 
struct A
{
   int v = 3;
};
 
 
namespace Foo
{
   template <int k=11> // note the default value of 11
   int operator+(A const& rhs, A const& lhs)
   {
      return rhs.v + lhs.v + k;
   }
}
 
using Foo::operator+; // will use k == 11
 
 
using namespace std;
int main()
{
 
   A a1, a2;
 
   cout << a1 + a2 << endl;
 
   return EXIT_SUCCESS;
}


The templated overloaded operator+ is brought into ADL scope with the
`using` clause and when used the default template value of 11 is used
to produce 3+3+11 = 17.

My question is how do I set a different k value, presumable where the
using clause is, so that the + syntax remains unchanged?

Thanks!
Adi


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Adi
4/27/2016 1:08:58 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

4 Replies
311 Views

Similar Articles

[PageSpeed] 24

{ quoted server banner and signature redacted by mod. -mod }

Obviously, the example below is a contrived for simplicity. The operator in
my 
actual code makes domain-specific sense. 
Essentially what I am trying to achieve a policy-based design for operators 
where the template parameter k is a policy-related value.

In the code I want to keep the original `cout` line unchanged and only
change the way the operator function behaves when brought into scope.

Adi


On Wednesday, April 27, 2016 at 9:10:10 PM UTC+3, Adi Shavit wrote:
> { edited by mod to shorten lines to ~70 characters. -mod }
> 
> Hi,
> 
>   The following code (http://ideone.com/e.js/6pTF0n) works fine:
> 
> #include <iostream>
>  
> struct A
> {
>    int v = 3;
> };
>  
>  
> namespace Foo
> {
>    template <int k=11> // note the default value of 11
>    int operator+(A const& rhs, A const& lhs)
>    {
>       return rhs.v + lhs.v + k;
>    }
> }
>  
> using Foo::operator+; // will use k == 11
>  
>  
> using namespace std;
> int main()
> {
>  
>    A a1, a2;
>  
>    cout << a1 + a2 << endl;
>  
>    return EXIT_SUCCESS;
> }
> 
> 
> The templated overloaded operator+ is brought into ADL scope with the
> `using` clause and when used the default template value of 11 is used
> to produce 3+3+11 = 17.
> 
> My question is how do I set a different k value, presumable where the
> using clause is, so that the + syntax remains unchanged?
> 
> Thanks!


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Adi
4/27/2016 5:12:54 PM
> Essentially what I am trying to achieve a policy-based design for
operators 
> where the template parameter k is a policy-related value.
> 
> In the code I want to keep the original `cout` line unchanged and only
> change the way the operator function behaves when brought into scope.
> 

Further digging shows that something like: `using Foo::operator+<42>` 
will not work. This is due to ISO C++ Standard 7.3.3.5: 
"A using-declaration shall not name a template-id."

Is there some way around this?

Adi


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Adi
4/28/2016 6:30:51 AM
> Further digging shows that something like: `using Foo::operator+<42>` 
> will not work. This is due to ISO C++ Standard 7.3.3.5: 
> "A using-declaration shall not name a template-id."
> 
> Is there some way around this?
> 

I managed to mostly get what I wanted by using a templated 
trait class and selectively typedefing it. See it in action here: 
<http://coliru.stacked-crooked.com/a/3c56e10fbb5a1d06>

More details here: <http://stackoverflow.com/a/36911214/135862>


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Adi
4/28/2016 3:01:24 PM
On 4/27/16 3:08 PM, Adi Shavit wrote:
> { edited by mod to shorten lines to ~70 characters. -mod }
>
> Hi,
>
>   The following code (http://ideone.com/e.js/6pTF0n) works fine:
>
> #include <iostream>
>
> struct A
> {
>    int v = 3;
> };
>
>
> namespace Foo
> {
>    template <int k=11> // note the default value of 11
>    int operator+(A const& rhs, A const& lhs)
>    {
>       return rhs.v + lhs.v + k;
>    }
> }
>
> using Foo::operator+; // will use k == 11
>
>
> using namespace std;
> int main()
> {
>
>    A a1, a2;
>
>    cout << a1 + a2 << endl;
>
>    return EXIT_SUCCESS;
> }
>
>
> The templated overloaded operator+ is brought into ADL scope with the
> `using` clause and when used the default template value of 11 is used
> to produce 3+3+11 = 17.
>
> My question is how do I set a different k value, presumable where the
> using clause is, so that the + syntax remains unchanged?
>
> Thanks!
> Adi
>
>
maybe something like:

namespace Foo2
{
    template <int k=13>
    int operator+(A const& rhs, A const& lhs) {
       return Foo::operator+<k>(rhs, lhs);
    }
}

using Foo2::operator+;


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Richard
5/8/2016 5:10:31 PM
Reply: