gpt4 book ai didi

c++ - 成员函数模板参数默认值

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:27:20 27 4
gpt4 key购买 nike

以下代码在 GCC 4.5.3 中编译但在 VS 2008 和 2010 中不编译。这是由于 VS 编译器错误还是标准禁止提供默认函数模板参数值?

#include <cstdlib>

struct Bar
{
enum Group{ A , B , C };
};

struct Foo
{
template<typename T>
static void getSome( typename T::Group = T::A );
};

template<typename T>
void Foo::getSome( typename T::Group )
{
};

int main()
{
Foo::getSome<Bar>(); // Does not compile in VS 2008 & 2010 (compiles in gcc 4.5.3)
Foo::getSome<Bar>( Bar::C ); // Compiles in VS 2008 and gcc 4.5.3
return EXIT_SUCCESS;
}

错误信息

prog.cpp(11) : error C2589: '::' : illegal token on right side of '::'
prog.cpp(11) : error C2059: syntax error : '::'

最佳答案

这是一个 MSVC 错误。

正如您可能猜到的那样,错误在于使用默认参数处理模板函数。

他们的解决方法是提供所有函数参数。 (恶心)

已确认 here .

关于c++ - 成员函数模板参数默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14812171/

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