簡體   English   中英

使用前向聲明類的嵌套(尚未定義)類型的部分模板特化

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

有一種方法可以為前向聲明(不完整)類型( answer )進行部分模板特化。 但是在看到提到的問題之后,我想知道是否可以使用不完整的嵌套(可能是私有)類為類模板定義部分特化。
在 C++ 中,目前無法在不定義類的情況下前向聲明嵌套類:

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

通過一些技巧,我制作了一個工作代碼(為了研究): 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;
}

但是對於private類型,使用了一個丑陋的朋友。 friend struct specialize<undefined::foo>; 在語義上會更具吸引力。

還有其他或更優雅的解決方案嗎?

一個更優雅/不那么復雜的解決方案: 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;
}

它允許通過延遲模板實例化引用尚未完成的類的不存在的成員類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM