问题描述
以下代码最能说明我的问题:
My issue is best demonstrated by the following code:
#include <fstream>
#include <iterator>
class Bar
{
public: template <class Iterator> Bar(Iterator first, Iterator last) {}
};
void foo(const Bar& bar) { }
int main(int argc, char** argv)
{
std::ifstream file("file.txt");
Bar bar(std::istream_iterator<char>(file), std::istream_iterator<char>());
foo(bar); // error C2664: 'foo' : cannot convert parameter 1 from 'Bar (__cdecl *)(std::istream_iterator<_Ty>,std::istream_iterator<_Ty> (__cdecl *)(void))' to 'const Bar &'
// with
// [
// _Ty=char
// ]
// Reason: cannot convert from 'overloaded-function' to 'const Bar'
// No constructor could take the source type, or constructor overload resolution was ambiguous
return 0;
};
以下是一些类似的 bar
实例,不会导致同样的歧义:
Here are some similar instantiations of bar
that don't cause the same ambiguity:
Bar bar = Bar(std::istream_iterator<char>(file), std::istream_iterator<char>());
和
std::istream_iterator<char> start(file);
std::istream_iterator<char> end;
Bar bar(start, end);
我的问题是,导致它被误解的 bar
的第一个声明是什么?
My question is, what is it about the first declaration of bar
that causes it to be misinterpreted?
注意:我正在使用 Visual Studio 2010 (10.0.30319.1) 进行测试
note: I'm testing with Visual Studio 2010 (10.0.30319.1)
推荐答案
ArunMu 获得部分功劳,它确实是 最令人烦恼的解析,但该术语是在 Meyer 的 Effective STL(第 1 章,第 6 项)中创造的,而不是 Exceptional C++.
ArunMu gets partial credit, it is indeed an example of Most Vexing Parse, but that term was coined in Meyer's Effective STL (Chapter 1, Item 6) not Exceptional C++.
它是被解释为一个函数指针(错误的(__cdecl *)
部分是一个致命的放弃),显然C++标准要求它被这样解释.有人对此有章节/诗句引文吗?
It is being interpreted as a function pointer (the (__cdecl *)
portion of the error is a dead give away), and apparently the C++ standard requires it to be interpreted that way. Does anyone have a chapter/verse citation for that?
还有另一种解决方案来消除歧义.在每个参数周围添加一组额外的括号也有效:
There is also a another solution to provide a disambiguation. Adding an additional set of parenthesis around each parameter works too:
Bar bar( (std::istream_iterator<char>(file)), (std::istream_iterator<char>()) );
同样值得指出的是,这个问题与我最初认为的模板无关.
It's also worth pointing out that the issue is unrelated to the templates, as I had originally thought.
这篇关于使用模板化构造函数解释为函数定义的非模板化类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!