0

I want to create aliases for a few of the Range-V3 functions/functors into my own namespace. For the functors like begin, end, it's just doing auto constexpr begin = ::ranges::begin. However, for a function like copy, I'm not sure how to proceed. I've tried using ::ranges::copy(); and using ::ranges::copy; but neither work.

Any help would be much appreciated! A small example of what I'm doing:

namespace example::ranges
{
   auto constexpr begin = ::ranges::begin;
   auto constexpr end = ::ranges::end;
   auto constexpr size = ::ranges::size;
   using ::ranges::copy(); // Doesn't work
}  
3
  • What is ::ranges::copy? A function, a class, a typedef,... ? Maybe already a using? Commented Oct 4, 2018 at 16:04
  • Is there a reason why you only want to create "a few" aliases? Otherwise it would be as simple as namespace example { namespace ranges = ::ranges; }. Commented Oct 4, 2018 at 16:12
  • 1
    using ::ranges::copy; didn't work? Really? Commented Oct 4, 2018 at 16:52

1 Answer 1

3

Unfortunately there is no straightforward way to create aliases for functions in C++ (yet). Your best bet is creating a perfectly-forwarding wrapper:

namespace example::ranges
{
    template <typename... Ts>
    auto begin(Ts&&... xs)                   
        noexcept(noexcept(::ranges::begin(std::forward<Ts>(xs)...))) 
              -> decltype(::ranges::begin(std::forward<Ts>(xs)...)) {
                   return ::ranges::begin(std::forward<Ts>(xs)...);
    }
}

This will behavely as closely as possible to the original function, retaining its noexcept-ness, SFINAE-friendliness, and overloaded behavior. The triplication can be simplified with a macro.

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

3 Comments

Ahhh I was hoping that wouldn't be the case. What do you mean by "yet"? Is it planned for C++20?
@brrrlinguist: this was discussed in Rapperswil, but there was no consensus
I think the noexcept specifier should appear before the trailing return type. Can you please fix it?

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.