简体   繁体   中英

Partial template specialization using nested (undefined yet) type of a forward-declared class

There is a way to make a partial template specialization for a forward-declared (incomplete) type ( answer ). But after seeing the mentioned question I wondered if it is possible to define a partial specialization for a class template using an incomplete nested (and possibly private) class.
In C++ currently one can't forward-declare a nested class without defining the class:

class undefined;
class undefined::foo; // impossibru

With some hacks I made a working code (for a sake of research): https://godbolt.org/z/9W8nfhx8P

#include <iostream>

template <typename T, typename = T>
struct specialize;

// workaround to get to a nested foo
template <typename T, typename...>
struct mem_foo
{
    // error: 'struct undefined::foo' is private within this context
    using type = typename T::foo;
};

class undefined;
// class undefined::foo; // impossibru

template <typename MemFoo>
struct specialize<typename mem_foo<undefined, MemFoo>::type, MemFoo>
{
    void operator()(const MemFoo &f) const 
    {
        // this will compile however
        std::cout << f.name << std::endl;
    }
};

#include <string>

class undefined
{
private: // will not compile without friend
    struct foo{
        std::string name = "John Cena";
    };

    friend struct mem_foo<undefined, foo>; // ugly 
    // friend struct specialize<foo>; // this is irrelevant, but would be nicer than mem_foo

public:
    static foo get() { return {}; }
};

int main()
{
    specialize</*undefined::foo*/decltype(undefined::get())>{}(undefined::get());

    return 0;
}

But for a private types an ugly friend is used. friend struct specialize<undefined::foo>; would be more semantically appealing.

Is there another or more elegant solution?

A more elegant/less convoluted solution: https://godbolt.org/z/3vrfPWP5f

#include <iostream>

template <typename T, typename = T>
struct specialize;

template <typename T, typename ...>
struct defer_instantiation
{
    using type = T;
};

template <typename T, typename ... R>
using defer_instantiation_t = typename defer_instantiation<T, R...>::type;

class undefined;
// class undefined::foo; // impossibru

template <typename MemFoo>
struct specialize<typename defer_instantiation_t<undefined, MemFoo>::foo, MemFoo>
{
    void operator()(const MemFoo &f) const 
    {
        // this will compile however
        std::cout << f.name << std::endl;
    }
};

#include <string>

class undefined
{
private:
    struct foo{
        std::string name = "John Cena";
    };

    friend struct specialize<foo>; // this is irrelevant, but would be nicer than mem_foo

public:
    static foo get() { return {}; }
};

int main()
{
    specialize</*undefined::foo*/decltype(undefined::get())>{}(undefined::get());

    return 0;
}

It allowes to reference yet non-existing member types of a yet incomplete class via a deferred template instantiation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM