using declarations and operator overloading?

S

Steven T. Hatton

Suppose I overload an operator in namespace A. How can I introduce it into
another namespace B? The following fails with the error shown
subsequently:

#include <iostream>
#include <functional>
#include <boost/array.hpp>

namespace A {
namespace {
using boost::array;
}

template<typename T, size_t Size_S>
array<T,Size_S>& operator+=(array<T,Size_S>& lhs, const array<T, Size_S>&
rhs)
{
std::transform(lhs.begin(), lhs.end()
, rhs.begin()
, lhs.begin()
, std::plus<T>());
return lhs;
}
}

namespace B {
namespace {
using boost::array;
using A::eek:perator+=(array<T,Size_S>& lhs, const array<T, Size_S>& rhs);
}

void f() {
array<double, 4> a = {1,2,3,4};
array<double, 4> b = {5,6,7,8};
a+=b;
copy(a.begin(),a.end(),std::eek:stream_iterator<double>(std::cout," "));
std::cout << std::endl;
}
}

int main(){
B::f();
}

-*- mode: compilation; default-directory: "~/code/c++/scratch/stl-ops/" -*-
g++ -oopassign opassign.cc -I$CPPSTH -I$BOOST_INCLUDE
opassign.cc:22: error: syntax error before `array'
opassign.cc: In function `void B::f()':
opassign.cc:28: error: no match for 'operator+=' in 'a += b'
distcc[2235] ERROR: compile on localhost failed

Compilation exited abnormally with code 1 at Tue Nov 9 23:07:37


--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
 
J

Jonathan Turkanis

Steven T. Hatton said:
Suppose I overload an operator in namespace A. How can I introduce it into
another namespace B? The following fails with the error shown
subsequently:

#include <iostream>
#include <functional>
#include <boost/array.hpp>

namespace A {
namespace {
using boost::array;
}

template<typename T, size_t Size_S>
array<T,Size_S>& operator+=(array<T,Size_S>& lhs, const array<T, Size_S>&
rhs)
{
std::transform(lhs.begin(), lhs.end()
, rhs.begin()
, lhs.begin()
, std::plus<T>());
return lhs;
}
}

namespace B {
namespace {
using boost::array;
using A::eek:perator+=(array<T,Size_S>& lhs, const array<T, Size_S>& rhs);

using A::eek:perator+=;
}

void f() {
array<double, 4> a = {1,2,3,4};
array<double, 4> b = {5,6,7,8};
a+=b;
copy(a.begin(),a.end(),std::eek:stream_iterator<double>(std::cout," "));
std::cout << std::endl;
}
}

int main(){
B::f();
}

Jonathan
 
V

Victor Bazarov

Steven T. Hatton said:
Suppose I overload an operator in namespace A. How can I introduce it
into
another namespace B? The following fails with the error shown
subsequently:

#include <iostream>
#include <functional>
#include <boost/array.hpp>

namespace A {
namespace {
using boost::array;
}

template<typename T, size_t Size_S>
array<T,Size_S>& operator+=(array<T,Size_S>& lhs, const array<T, Size_S>&
rhs)
{
std::transform(lhs.begin(), lhs.end()
, rhs.begin()
, lhs.begin()
, std::plus<T>());
return lhs;
}
}

namespace B {
namespace {
using boost::array;
using A::eek:perator+=(array<T,Size_S>& lhs, const array<T, Size_S>& rhs);
}

void f() {
array<double, 4> a = {1,2,3,4};
array<double, 4> b = {5,6,7,8};
a+=b;
copy(a.begin(),a.end(),std::eek:stream_iterator<double>(std::cout," "));
std::cout << std::endl;
}
}

int main(){
B::f();
}

-*- mode: compilation; default-directory:
"~/code/c++/scratch/stl-ops/" -*-
g++ -oopassign opassign.cc -I$CPPSTH -I$BOOST_INCLUDE
opassign.cc:22: error: syntax error before `array'
opassign.cc: In function `void B::f()':
opassign.cc:28: error: no match for 'operator+=' in 'a += b'
distcc[2235] ERROR: compile on localhost failed

Compilation exited abnormally with code 1 at Tue Nov 9 23:07:37

I removed the Boost stuff (for simplicity). If it's the Boost types
that are interfering, you're on your own, I don't have Boost installed.
----------------------------
namespace F {
class Foo {};
}

namespace A {
using F::Foo;
Foo operator+(Foo const&, Foo const&);
}

namespace B {
namespace {
using F::Foo;
using A::eek:perator+;

void f() {
Foo a, b;
a+b;
}
}
}

int main(){
B::f();
}
 
S

Steven T. Hatton

Jonathan Turkanis wrote:

using A::eek:perator+=;
Thanks. I must have typoed something the first time I tried that. It works
now.

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top