gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:40:44 26 4
gpt4 key购买 nike

在 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/2788765/

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