简体   繁体   English

使用前向声明类的嵌套(尚未定义)类型的部分模板特化

[英]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 ).有一种方法可以为前向声明(不完整)类型( 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:在 C++ 中,目前无法在不定义类的情况下前向声明嵌套类:

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

With some hacks I made a working code (for a sake of research): https://godbolt.org/z/9W8nfhx8P通过一些技巧,我制作了一个工作代码(为了研究): 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.但是对于private类型,使用了一个丑陋的朋友。 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一个更优雅/不那么复杂的解决方案: 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.它允许通过延迟模板实例化引用尚未完成的类的不存在的成员类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有预先声明的类的QSharedDataPointer - QSharedDataPointer with forward-declared class clang vs gcc模板子类在父级中使用前向声明的类 - clang vs gcc template child class using forward-declared class in parent 前向声明的集体成员的前向声明 - Forward declaration of a forward-declared class member 返回一个前向声明的结构未定义的行为? - Is returning a forward-declared structure undefined behavior? 正向声明的类型和“已经声明为类类型的非类类型” - Forward-declared type and “non-class type as already been declared as a class type” 具有前向声明类型的函数模板专业化 - Function Template Specialization with Forward Declared Type 使用模板化类型的部分模板专业化(嵌套模板类) - Partial template specialization, using a templatized type (nested template classes) 前向声明的类枚举问题的变通方法? - Workarounds for the forward-declared class enumeration problem? 在模板基类中的虚函数中使用前向声明的类,其中构造函数只需要前向声明? - Use a forward-declared class in a virtual function in a template baseclass where the constructor only needs the forward declare? 使用具有前向声明类型的模板 - 安全吗? - Using templates with forward-declared types - safe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM