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

标签 c++ templates function-templates default-arguments

以下代码在 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/

相关文章:

c++ - 需要帮助实现特殊的边缘检测器

c++ - 如何修改 CMakelists for Qt5 从 Windows 到 Linux?

c++ - 指向对象 X 的 weak_ptr 在对象 X 销毁期间是否仍然有效

java - 速度空值和空字符串

c++ - 使用类和仿函数作为模板参数实现通用二元函数

c++ - 使用基类的函数模板特化

c++ - 非尾随函数模板参数包的合法使用?

c++ - 解析类似 INI 的配置文件

c++ - 经典的 C++ 静态初始化顺序惨败重温

php - 在没有 Eval 的情况下在字符串中执行 PHP 代码