0

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

4
  • You can do a namespace alias (std::placeholder is a namespace, not a type), but it has to be at namespace or function scope: namespace RequestResultHandle = std::placeholder) Commented May 18, 2017 at 21:11
  • "No go" is not an acceptable problem statement. Commented May 18, 2017 at 21:16
  • This has nothing to do with std::placeholders (or std::placeholder, which doesn't exist), which you'd have found out if you'd played around and discovered that this problem is about how you're using namespace aliases (which cannot be at class scope). Commented May 18, 2017 at 21:17
  • Ah yes. std::placeholders is what I meant to type :) Thank you guys. Commented May 18, 2017 at 21:22

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

There is no such thing as std::placeholder.
@BoundaryImposition - Gee, begrudge me a mistyped 's' why don't you
This is actually the best answer now ^_^
2

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;
}

1 Comment

Right on. Yeah, API level probably not the best for any of this. It should be angled for more internal work among cohorts. Thanks for the input! I was just hoping the result, being of varying volition based on the model setData function, could utilize a 'pretty' word rather than using std::placeholders anywhere we need to apply a bind
0

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.

3 Comments

"You can't use the using directive to alias a namespace." No but with a trivial tweak (say namespace instead of union) you can do just that, without needing to name each namespace member in turn.
@BoundaryImposition, Did you just say union instead of using? You are loosing your touch :)
It's late haha :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.