模板类如何在 GCC 和 Clang 中继承子类

How is template class inherited child classes in GCC and Clang

本文关键字:Clang 继承 子类 GCC      更新时间:2023-10-16

我不明白下面的代码示例中的GCC和Clang行为。为什么GCC/Clang允许调用非继承的类,如check<'d'>。

#include <iostream>
template<char TCh> struct check { static constexpr char ch = TCh; };
template<char ... TChs> struct checker
    : public check<TChs>...
{};
using double_litters = checker<'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'>;
int main()
{
    std::cout << double_litters::check<'d'>::ch << std::endl;  // GCC/Clang compile OK, VS compile ERROR
    std::cout << double_litters::check<'1'>::ch << std::endl;  // GCC/Clang compile OK, VS compile OK
    return 0;
}

不确定为什么会发生这种情况,以及这是否真的是一个错误。但是你可以这样修复它:

double_litters().check<'d'>::ch

这将在 GCC 中给出错误,不要检查叮当...