std :: make_shared作为默认参数不编译 [英] std::make_shared as a default argument does not compile

查看:1177
本文介绍了std :: make_shared作为默认参数不编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual C ++(2008和2010)中,以下代码不会编译时出现以下错误:

  #include< ;存储器> 

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

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



错误C3861:'make_shared':未找到标识符



它抱怨P :: Foo不是:: Foo()。



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

解决方案

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

 命名空间ns 
{
template< typename T>
class test
{
};

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

//工作原理:
void f(ns :: test< int> = ns :: func< int>()){}

class test2
{
//不工作:
void g(ns :: test< int> = ns :: func< int>
{
}
}; Visual C ++ 2008和2010都报告:




< blockquote>

错误C2783:' ns :: test< T> ns :: func(void)'无法推导' T '的模板参数


Comeau对此代码没有问题。


In Visual C++ (2008 and 2010), the following code does not compile with the following error:

#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' : is not a member of '`global namespace''

error C3861: 'make_shared': identifier not found

It is complaining about the definition of P::Foo() not ::Foo().

Does anybody know why it is valid for Foo() to have a default argument with std::make_shared but not P::Foo()?

解决方案

It looks like a bug in the compiler. Here is the minimal code required to reproduce the problem:

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 and 2010 both report:

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

Comeau has no issues with this code.

这篇关于std :: make_shared作为默认参数不编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆