English 中文(简体)
模板函数查找失败
原标题:Failing the template function lookup

考虑以下示例。

#include <iostream>
#include <boost/optional.hpp>

template < typename A >
int boo( const boost::optional< A > &a );

template < typename A >
int foo( const A &a )
{
    return boo( a );
}

template < typename A >
int boo( const boost::optional< A > & )
{
    return 3;
}


int main()
{
    std::cout << "foo = " << foo( 3 ) << std::endl;
    std::cout << "boo = " << boo( 3 ) << std::endl;
}

使用g++4.3.0编译会引发下一个编译错误:

dfg.cpp: In function ‘int main()’:
dfg.cpp:25: error: no matching function for call to ‘boo(int)’
dfg.cpp: In function ‘int foo(const A&) [with A = int]’:
dfg.cpp:24:   instantiated from here
dfg.cpp:12: error: no matching function for call to ‘boo(const int&)’

What should I do differently (if possible with references from the C++ standard)? Why is it happening and how do I fix it?

编辑

修复方法是在<code>foo</code>中创建正确的类型:

template < typename A >
int foo( const A &a )
{
    const boost::optional< A > optA( a );
    return boo( optA );
}

但问题仍然存在:为什么它不是自动创建的?

最佳回答
return boo( a );

这里a的类型是int,并且没有名为boo、接受int类型参数的函数。因此,您看到了这个错误:

dfg.cpp:25: error: no matching function for call to ‘boo(int)’

即使int可以被隐式转换为boost::optional<;整数>,编译器无法推导boost::optional<;的模板参数;T>。这是一种非推导上下文,其中显式需要提及类型为,

   return boo<A>(a);

《标准》在14.8.1.1美元中说,

if a template-parameter is not used in any of the function parameters of a function template, or is used only in a non-deduced context, its corresponding template-argument cannot be deduced from a function call and the template-argument must be explicitly specified.

问题回答

要解决此问题,您需要在调用boo时显式指定类型,即。

return boo<A>( a );

std::cout << "boo = " << boo<int>( 3 ) << std::endl;

编辑:删除了我的解释,这是垃圾,纳瓦兹的解释更好。。

您假设,因为<code>可选<;整数>有一个来自int的隐式构造函数,编译器应该知道这是您试图创建的类型。

模板类型推导不扩展到该类型。

您可以编写自己的boo模板,其中包含一个将可选作为概括的模板

template< typename A > int boo( const A& a );
template < typename A >
int boo( const boost::optional< A > & )
{
    return 3;
}

template < typename A >
int boo( const A & a )
{
    return boo<A>(boost::optional(a)); // allows implicit conversion
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签