Quantcast
Viewing all articles
Browse latest Browse all 22122

C++ resolve upper scope type at callsite [duplicate]

does anybody know why the following code snippet does not compile?

struct A
{
    struct Options
    {
        int a;
        int b;
    };

    A(const Options& opt) : opt_(opt) {}
    Options opt_;
};

template <typename T>
T build(const typename T::Options& options)
{
    return T(options);
}

int main()
{
    A::Options opt;
    auto test = build(opt);
}

GCC returns the following error: error: no matching function for call to 'build(A::Options&) although it seems (to me) like a matching function call is right there.

I can get the code to compile if I add a connection from Options back to A like so:

struct A
{
    struct Options
    {
        using Parent = A;
        int a;
        int b;
    };

    A(const Options& opt) : opt_(opt) {}
    Options opt_;
};

template <typename T>
auto build(const T& options)
{
    return typename T::Parent(options);
}

int main()
{
    A::Options opt;
    auto test = build(opt);
}

however, I'm making some modifications to existing framework where I would rather not add the A::Options::Parent hook in a bunch of places. Ideally, I would like there to be some way I could change the build function to get my desired behavior.

At the very least, this behavior is surprising to me. So any information is appreciated! Thank you.


Viewing all articles
Browse latest Browse all 22122

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>