gpt4 book ai didi

c++ - 嵌套模板显式特化

转载 作者:搜寻专家 更新时间:2023-10-31 02:04:51 25 4
gpt4 key购买 nike

我有一些编译器行为 - 在 VisualC++ 和 g++ 上不同 - 我不明白(对于任何一个编译器)。我会用英语描述它,但也许只看下面的代码会更容易。

它与具有成员类模板和成员函数模板的类模板有关。我正在尝试同时

  1. 显式特化外部类模板(不特化成员模板)
  2. 在外部类模板的显式特化显式特化成员模板。

我发现,对于这两个编译器,如果我 #1(显式特化外部类模板)或#2(显式特化成员模板),一切都可以正常编译(并且实例化符合预期) .

但是如果我尝试同时执行#1 和#2(我相信以正确的顺序声明),我发现

  • 使用 g++,我可以为成员类模板执行此操作,但对于成员模板类声明会出现编译错误。
  • 对于 VC++,两个成员模板都出现编译器错误。

这是外部类模板的主要定义。在以下所有情况下都是一样的:

// Class template with a member class template and a member function template
template <int I>
struct OuterClass
{
template <int J> struct InnerClass {};
template <int J> static void InnerFunc() {}
};

这里只做#1(显式特化外部类模板)。这编译得很好,实例化符合预期。

// Explicit specialization of outer class template,
// leaving member templates unspecialized.
template <>
struct OuterClass<1>
{
template <int J> struct InnerClass {};
template <int J> static void InnerFunc() {}
};

此处仅执行#2(显式特化成员模板)。这编译得很好,实例化符合预期。

// Explicit specialization of inner templates for
// an explicit specialization of outer class template
template <> template <> struct OuterClass<1>::InnerClass<1> {};
template <> template <> void OuterClass<1>::InnerFunc<1>() {}

这里尝试同时执行 #1 和 #2 - 只需将前面的两个代码片段粘贴在一起:

// Explicit specialization of outer class template,
// leaving member templates unspecialized.
template <>
struct OuterClass<1>
{
template <int J> struct InnerClass {};
template <int J> static void InnerFunc() {}
};

// Explicit specialization of inner templates for
// an explicit specialization of outer class template
template <> template <> struct OuterClass<1>::InnerClass<1> {}; // Line A
template <> template <> void OuterClass<1>::InnerFunc<1>() {} // Line B

g++ 可以很好地编译“A 行”(并且实例化符合预期)。但是 g++ 为 B 行给出了一个编译器错误:“太多的模板参数列表”。

VC++ 给出了“A 行”和“B 行”的编译器错误(太乱了,太多了,无法在这里重述)。

同样,对于两个编译器,“A 行”和“B 行”都可以正常编译,因为它们在外部类模板的显式特化之后没有出现。

以我的理解,一切都应该编译正常。那么谁是对的——我、g++ 还是 VC++?更重要的是,为什么?

请理解这不是一个“我如何完成 X”的问题,而是一个“我想完全理解 C++”的问题。如果您花时间通读并思考一下,我将向您表示感谢……我希望我尽可能地把它归结起来。

最佳答案

我在这个问题上花了很多时间,我想我已经弄清楚了 - 我所说的“弄清楚”是指我确切地知道在我试过的两种编译器(MSVC 和g++),以及每个编译器使用什么语法。这一切都很丑陋,但它是可预测和可重复的——我有很多此处未显示的示例代码,我从中推导出了结果。为了清楚起见并避免让我失望,这里的描述不是我的理论,它是在数百个示例案例中观察到的编译器行为。

在高层次上:

  • MSVC 中有一个错误 (?) 会阻止编译,在某些明确定义的情况下函数 嵌套在类中的模板的完全显式特化模板。
  • 两个编译器总是可以编译嵌套在另一个类模板中的类模板的显式特化。但是,“template<>”必须出现多少次的规则对于 MSVC 和 g++ 是不同的。因此,对于可移植代码,在某些情况下您必须使用条件编译。

我在这里给出的详细描述可能太简短了,普通读者可能无法理解,但我还是会尝试一下。

  • 通常,在声明/定义类模板成员的特化时(可能在嵌套类模板的深层链中),“template<>”必须出现的次数等于“特化”的次数从最近的类模板特化“覆盖”被声明的特化到被声明的特化。
    • 将其称为“最后一跳规则”
    • 这适用于两种编译器上的所有类型的可特殊化成员(类/类模板、函数/函数模板、变量/变量模板和枚举),只有一个异常(exception)(见下文)
  • 用于声明/定义嵌套在另一个类模板中的 模板的特化(可能在嵌套类模板的深层链中)
    • 对于 MSVC:“template<>”必须出现的次数是“最后一跳规则”
    • 对于 g++:这是“Las Hop 规则”不适用的一种情况。 (无论出于何种原因,我猜是 g++ 中的错误?)。在这种情况下,“template<>”必须出现的次数等于声明的特化的“深度”减去覆盖声明的特化的模板类特化的总数。
  • 用于声明/定义嵌套在类模板中的函数 模板的特化(可能在嵌套类模板的深层链中)
    • 对于 MSVC:在某些情况下无法编译。我推导出条件会编译,什么时候不会编译,但是这里描述起来太复杂了。我想 MSVC 中有一个错误——g++ 总是有效。当它起作用时,“模板<>”出现在“最后一跳规则”中的次数(对于两个编译器)。
    • 对于 g++:这总是有效的。 “模板<>”出现在“最后一跳规则”中的次数。

这里是一些示例代码,显示了在定义具有特定特化链的嵌套类模板时“template<>”必须出现多少次。这在 MSVC 和 g++ 上编译(使用条件编译)。此代码不涉及嵌套的函数模板。

#include <boost\predef.h>  // For conditional compilation

// Nested class templates, 3 deep.
template <int I1> struct T1
{
template <int I2> struct T2
{
template <int I3> struct T3 {};
};
};

// Specialization of the third level of class template.
// "template<>" appears three times here for both MSVC and g++ -
// in this case the rules for both compilers both yield 3.
// Note this class template specialization nests another 2 levels of class templates.
template <> template <> template<> struct T1<1>::T2<1>::T3<1>
{
template <int I4> struct T4
{
template <int I5> struct T5 {};
};
};

// Specialize the class template contained in the class template specialization above.
// In this case, the number of times "template<>" must appear differs between MSVC and g++,
// so conditional compilation is used.
#if BOOST_COMP_GNUC
// According to the rule described for g++, "template<>" must appear 4 times:
// (Overall specialization level of 5) - (1 covering specialization which is T1<1>::T2<1>::T3<1>) = 4
template <> template<> template<> template<> struct T1<1>::T2<1>::T3<1>::T4<1>::T5<1>
#elif BOOST_COMP_MSVC
// MSVC uses the last hop specialization rule, so "template<>" must appear 2 times -
// because the closest covering specialization, T1<1>::T2<1>::T3<1>, is two hops back.
template <> template<> struct T1<1>::T2<1>::T3<1>::T4<1>::T5<1>
#else
#error Unsupported compiler!
#endif
{
//...
}

关于c++ - 嵌套模板显式特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52899059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com