c++ - std::make_shared 作为默认参数不编译

标签 c++ visual-c++ templates visual-c++-2008 visual-c++-2010

在 Visual C++(2008 和 2010)中,以下代码无法编译并出现以下错误:

#include <memory>

void Foo( std::shared_ptr< int >    test = ::std::make_shared< int >( 5 ) )
{
}

class P
{
    void
    Foo( std::shared_ptr< int > test = ::std::make_shared< int >( 5 ) )
    {
    }
};

error C2039: 'make_shared' : 不是 '`global namespace'' 的成员

error C3861: 'make_shared': 找不到标识符

它提示 P::Foo() 而不是::Foo() 的定义。

有谁知道为什么 Foo() 有一个默认参数与 std::make_shared 而不是 P::Foo() 是有效的?

最佳答案

这看起来像是编译器中的错误。以下是重现该问题所需的最少代码:

namespace ns
{
    template <typename T>
    class test
    {
    };

    template <typename T>
    test<T> func()
    {
        return test<T>();
    }
}

// Works:
void f(ns::test<int> = ns::func<int>()) { }

class test2
{
    // Doesn't work:
    void g(ns::test<int> = ns::func<int>()) 
    { 
    }
};

Visual C++ 2008 和 2010 均报告:

error C2783: 'ns::test<T> ns::func(void)' : could not deduce template argument for 'T'

Comeau 对此代码没有任何问题。

关于c++ - std::make_shared 作为默认参数不编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15622475/

相关文章:

常量数据持有者类的 C++ 模板

c++ - 我不理解 std::result_of 和 decltype 的这些用法

c++ - 具有特征类型输出的特征二进制表达式

c++ - '[' 标记之前的预期主表达式

c++ - 由于某种原因,我的 ram 上的 int 重量超过 32 位

c++ - 通过if else在函数中定义变量

c++ - 重新定义默认参数: parameter 2

c++ - 在预处理器指令中使用模板参数?

c++ - 使用 OCILIB 调用带有输出变量的 Oracle 过程

c++ - 漂亮地打印 C++ 中的变量和值列表