2

I would like to use a using declaration to enable ADL on a specific function lookup so that I can use it in a constructor initialization list.

Here is the code:

template< typename T >
struct RefWrapper
{
    RefWrapper(T& t)
    : w(t)
    {}

    T& w;
};

struct Val
{
    Val(RefWrapper<Val> rw)
    {/*...*/}
};

namespace X
{
    template< typename T >
    RefWrapper<T> MakeWrap(T& t)
    { return RefWrapper<T>(t); }
}

namespace Y
{
    using X::MakeWrap;  // god no !
    struct C
    {
        //using X::MakeWrap;  // I want it here
        //                     error: using-declaration for non-member at class scope

        C(Val& value) : member(MakeWrap(value))
        {}

        Val member;
    };
}

related:
How narrow should a using declaration be?
In this question's unique's answer, (4) is impossible, but it is the place I want !

Community
  • 1
  • 1
v.oddou
  • 6,476
  • 3
  • 32
  • 63

1 Answers1

1

Unfortunately, you can't do that.

N4527::7.3.3$3, The using declaration, [namespace.udecl]:

In a using-declaration used as a member-declaration, the nested-name-specifier shall name a base class of the class being defined.

Of course, you can explicitly appoint the nested-name-specifier like:

C(Val& value) : member(X::MakeWrap(value))

Or as a workaround, you can use a local wrapper function, something like this:

struct C
{
    //using X::MakeWrap;  // I want it here
    //                     error: using-declaration for non-member at class scope

    C(Val& value) : member(MakeWrap(value))
    {}

    Val member;
private:
    template< typename T >
    T MakeWrap(T& t) { return X::MakeWrap(t); }

};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • damn, my question does not attract any curiosity at all. well, I hoped there could be some workaround practice for this case. – v.oddou Jul 15 '15 at 09:45
  • 1
    @v.oddou As a workaround, you can use a local wrapper function. I've added it to my answer. – songyuanyao Jul 15 '15 at 09:57
  • indeed the local wrapper might be a reasonable workaround. good idea. Other than that, no chance for a more direct C+11 goodies ? because the function is nice but it has 2 drawbacks, 1 is verbosity, 2 is average guarantees regarding perfect forwarding for the argument and copy elision for the returned value. – v.oddou Jul 16 '15 at 01:39
  • @v.oddou Basically, the wrapper function would be an `inline` function, the 2nd problem should be okay. Verbosity...yes, it is. – songyuanyao Jul 16 '15 at 01:52