Groups | Search | Server Info | Login | Register


Groups > comp.lang.c++.moderated > #7435

Templated Overloaded Operator

Message-ID <704de14f-5d70-4106-b26d-fcc27fc78f65@googlegroups.com> (permalink)
Newsgroups comp.lang.c++.moderated
From Adi Shavit <adishavit@googlemail.com>
Subject Templated Overloaded Operator
Organization unknown
Date 2016-04-27 13:08 -0600

Show all headers | View raw


{ 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! ]

Back to comp.lang.c++.moderated | Previous | NextNext in thread | Find similar


Thread

Templated Overloaded Operator Adi Shavit <adishavit@googlemail.com> - 2016-04-27 13:08 -0600
  Re: Templated Overloaded Operator Adi Shavit <adishavit@googlemail.com> - 2016-04-27 17:12 -0600
    Re: Templated Overloaded Operator Adi Shavit <adishavit@googlemail.com> - 2016-04-28 06:30 -0600
      Re: Templated Overloaded Operator Adi Shavit <adishavit@googlemail.com> - 2016-04-28 15:01 -0600
  Re: Templated Overloaded Operator Richard Damon <Richard@Damon-Family.org> - 2016-05-08 17:10 -0600

csiph-web