mccatnm
mccatnm

Reputation: 222

using/typedef of namespace in header

I have a use case for the std::placeholder in a test application but am wondering if, in an effort to make things look a little cleaner on an API side, there is a way to bend using, typedef or even #define to alias the namespace at the header level.

// .../datarequestprocessor.h
class DataRequestProcessor {
public:
    using ProcessFunction = std::function<void(const ResultData &)>;
    using RequestResultHandle = std::placeholders; // No go. Same with ::_1
    ...
};

// ../datarequestprocessor.cpp
ProcessFunction DataRequestProcessor::prepOne()
{
    auto func = std::bind( &DataModel::setData,
                           m_model,
                           RequestResultHandle::_1 );
    return func;
}
... // For other variations.

This is purely semantic and also just an effort to learn about the nature of the using keyword. More of a learning experience then a real world application proposition.

Cheers

Upvotes: 0

Views: 1241

Answers (3)

If you want it at the header level, then it's a simple matter of introducing a namespace alias:

namespace RequestResultHandle = std::placeholders;

The above won't be accepted inside a class however.

Upvotes: 2

R Sahu
R Sahu

Reputation: 206567

You can't use the using directive to alias a namespace.

If you are motivated, you can define your own namespace in which you can alias specific types from another namespace.

namespace RequestResultHandle
{
    using _1 = std::placeholders::_1;
    using _2 = std::placeholders::_2;

    ...
}

Now, you can use RequestResultHandle::_1 instead of std::placeholders::_1.

Upvotes: 0

Brian Bi
Brian Bi

Reputation: 119069

in an effort to make things look a little cleaner on an API side

Ideally, you shouldn't be exposing placeholders in the API at all. If you are doing so, you haven't shown it in your code above.

If you are just using placeholders in the implementation, the following will do the trick:

ProcessFunction DataRequestProcessor::prepOne()
{
    using namespace std::placeholders;
    auto func = std::bind( &DataModel::setData,
                           m_model,
                           _1 );
    return func;
}

Upvotes: 2

Related Questions