Question

Consider a class that has a private std::vector data member:

class MyClass 
{
    private: 
        std::vector<double> _data;
    public:
        template <class... Args>
        /* something */ insert(Args&&... args) /* something */
        {
            return _data.insert(std::forward<Args>(args)...);
        }
};

What is the correct syntax (using C++14 auto/variadic templates/forward...) to transfer a given function of _data to MyClass (for example insert here) and provide the same interface for the user?

Was it helpful?

Solution

The correct syntax is this:

class MyClass 
{
    private: 
        std::vector<double> _data;
    public:
        template <class... Args>
        decltype(auto) insert(Args&&... args)
        {
            return _data.insert(std::forward<Args>(args)...);
        }
};

However, you don't actually need C++14 to do it. You can just use the C++11 syntax.

class MyClass 
{
    private: 
        std::vector<double> _data;
    public:
        template <class... Args>
        auto insert(Args&&... args) 
        -> decltype(_data.insert(std::forward<Args>(args)...))
        {
            return _data.insert(std::forward<Args>(args)...);
        }
};

OTHER TIPS

To truly forward a call to a member function, one must consider the necessity of correctly forwarding the *this value for the member call.

The following:

template<typename Type>
struct Fwd {
    Type member;

    template<typename ...Args>
    decltype(auto) Func(Args&&... args)
        noexcept(noexcept(member.Func(std::forward<Args>(args)...)))
        { return member.Func(std::forward<Args>(args)...); }
};

is sufficient for forwarding arguments and the exception specification, as you might have guessed. But it is insufficient to perfect forward *this:

struct S {
    // These overloads are reachable through Fwd<S>::Func().
    void Func(int) & {}
    void Func(int&&) & {}

    // These other overloads are not.
    void Func(int) const&;
    void Func(int&&) const&;
    void Func(int) volatile&;
    void Func(int&&) volatile&;
    void Func(int) const volatile&;
    void Func(int&&) const volatile&;
    void Func(int) &&;
    void Func(int&&) &&;
    // (These are rather uncommon, just provided for completude.)
    void Func(int) const&&;
    void Func(int&&) const&&;
    void Func(int) volatile&&;
    void Func(int&&) volatile&&;
    void Func(int) const volatile&&;
    void Func(int&&) const volatile&&;
};

There are two solutions for this problem. One is to simply create each and every other possible overload manually, possibly with a macro:

#define FWD(member, Func) \
    template<typename ...Args> \
    decltype(auto) Func(Args&&... args) & \
        noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
        { return member.Func(std::forward<Args>(args)...); } \
    \
    template<typename ...Args> \
    decltype(auto) Func(Args&&... args) const& \
        noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
        { return member.Func(std::forward<Args>(args)...); } \
    \
    template<typename ...Args> \
    decltype(auto) Func(Args&&... args) volatile& \
        noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
        { return member.Func(std::forward<Args>(args)...); } \
    \
    template<typename ...Args> \
    decltype(auto) Func(Args&&... args) const volatile& \
        noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
        { return member.Func(std::forward<Args>(args)...); } \
    \
    template<typename ...Args> \
    decltype(auto) Func(Args&&... args) && \
        noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
        { return std::move(member).Func(std::forward<Args>(args)...); } \
    \
    template<typename ...Args> \
    decltype(auto) Func(Args&&... args) const&& \
        noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
        { return std::move(member).Func(std::forward<Args>(args)...); } \
    \
    template<typename ...Args> \
    decltype(auto) Func(Args&&... args) volatile&& \
        noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
        { return std::move(member).Func(std::forward<Args>(args)...); } \
    \
    template<typename ...Args> \
    decltype(auto) Func(Args&&... args) const volatile&& \
        noexcept(noexcept(member.Func(std::forward<Args>(args)...))) \
        { return std::move(member).Func(std::forward<Args>(args)...); }

template<typename Type>
struct Fwd {
    Type member;
    FWD(member, Func)
};

The other solution is to avoid a member function altogether and use a free function:

template<typename Fwd, typename ...Args>
decltype(auto) Func(Fwd&& fwd, Args&&... args)
    noexcept(noexcept(std::forward<Fwd>(fwd).Func(std::forward<Args>(args)...))) {
    return std::forward<Fwd>(fwd).Func(std::forward<Args>(args)...);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top