Ambiguous overload accessing argument-less template functions with variadic parameters

久未见 提交于 2019-12-08 01:06:49

问题


Yeah, the title can scare babies, but it's actually quite straightforward.

I am trying to store a function pointer to a specialized template function, namely boost::make_shared (boost 1.41), as illustrated:

boost::shared_ptr<int> (*pt2Function)() = boost::make_shared<int>;

However, it won't compile (GCC 4.4.1) due to the fact that boost::make_shared has the following two specializations which the compiler can't tell apart in this context:

template< class T > boost::shared_ptr< T > make_shared()
...
template< class T, class... Args > boost::shared_ptr< T > make_shared( Args && ... args )

The error, for reference:

In function ‘int main()’:
error: converting overloaded function ‘make_shared’ to type ‘class boost::shared_ptr<int> (*)()’ is ambiguous
boost/smart_ptr/make_shared.hpp:100: error: candidates are: boost::shared_ptr<X> boost::make_shared() [with T = int]
boost/smart_ptr/make_shared.hpp:138: error:                 boost::shared_ptr<X> boost::make_shared(Args&& ...) [with T = int, Args = ]

If I comment out the non-variadic variation, the code compiles fine.

Does anyone know the proper syntax for resolving the ambiguity between two argument-less functions like this?


回答1:


Variadic template arguments mean you take 0..n template arguments, thus both your versions are matches.
You could resolve the ambiguity by adding another template parameter to the second version, so that it takes 1..n arguments.
Something like this should work:

template< class T, class Arg1, class... Args > 
boost::shared_ptr< T > make_shared(Arg1&& arg1, Args && ... args )

But as UncleBens correctly pointed out, you don't even need two versions. The following should be enough in your case:

template< class T, class... Args > 
boost::shared_ptr<T> make_shared(Args && ... args );

If you use only one template argument (i.e. T), you get the 0-argument version of make_shared().



来源:https://stackoverflow.com/questions/2109191/ambiguous-overload-accessing-argument-less-template-functions-with-variadic-para

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!