Using namespace in a class

P

PengYu.UT

Hi,

I have the following code. However, the lambda expression becomes too
complicated, because I put "boost::lambda::" before _1 and _2.

To make it more readable, I can uncomment the commented line
"using ...". However, this would pollute the global namespace, which
is undesirable.

I'm wondering if there is any way to enable a namespace inside a class
just like enable a namespace in another namespace?

Thanks,
Peng

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/core.hpp>
#include <iostream>

//using namespace boost::lambda;

struct A {
int doit() {
int a = 10;
return (boost::lambda::_1 + boost::lambda::_2)(a, a);
}
};

int main() {
A a;
std::cout << a.doit() << std::endl;
}
 
P

Piyo

struct A {
int doit() {
// use the namespace inside the enclosing scope
using namespace boost::lambda;
// or bring in the structs that you want to use
using boost::lambda::_1;
using boost::lambda::_2;
 
N

Noah Roberts

Piyo said:
// use the namespace inside the enclosing scope
using namespace boost::lambda;
// or bring in the structs that you want to use
using boost::lambda::_1;
using boost::lambda::_2;

or...


namespace l = boost::lambda;

return (l::_1 + l::_2)(a,a)...

or

return (l::_1 + l::_1)(a)
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top