{ 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!
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?
Essentially what I am trying to achieve a policy-based design foroperators
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.
{ 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
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 146:25:02 |
| Calls: | 12,090 |
| Calls today: | 3 |
| Files: | 15,000 |
| Messages: | 6,517,503 |