小编may*_*rne的帖子

如果无法编译未使用的函数,模板类可以使用 C++17 进行编译,但不能使用 C++20 进行编译

以下代码在选择 C++ 17 作为语言的 VS2019(版本 16.11.15)中编译没有错误。但它在 C++ 20 中失败,并出现错误“错误 C2027:使用未定义类型‘Anon’”

template <typename T> class a_template
{
public:
    void do_something(class Anon& anon) const;
};

template <typename T> void a_template<T>::do_something(class Anon& anon) const
{
    anon.do_something();
}
Run Code Online (Sandbox Code Playgroud)

该类Anon当然是未定义的,但 ::do_something 函数未使用,因此不需要实例化。这在 C++17 中没问题,但在 C++20 中显然不行。

这是语言规则的改变吗?如果是这样,是否可以在不实际定义的情况下修复它Anon

c++ templates c++17 c++20

3
推荐指数
1
解决办法
554
查看次数

标签 统计

c++ ×1

c++17 ×1

c++20 ×1

templates ×1