c++ - C++ 奇怪的模板/命名空间行为
问题描述
我在编译代码时遇到问题,因为它无法在模板上找到匹配的函数。我已将问题缩小到此示例:
namespace cv
{
class FileNode
{ };
template<typename _Tp> static inline void operator >> (const FileNode& n, _Tp& value)
{
read(n, value, _Tp());
}
static inline void read(const FileNode& node, bool& value, bool default_value)
{ }
}
class K
{ };
namespace D
{
class A
{ };
}
template<class X>
static void read(const cv::FileNode& node, X& x, const X& default_value)
{
return;
}
using namespace D;
class B
{
void read(const cv::FileNode& fn)
{
A a;
fn >> a;
}
};
int main(int argc, char* argv[]) { }
在 Gcc 9.10 上,我收到以下错误:
invalid initialization of reference of type 'bool&' from expression of type 'D::A' { read(n, value, _Tp()); }
在 Visual Studio 2019 上:
Error C2664 'void cv::read(const cv::FileNode &,bool &,bool)': cannot convert argument 2 from '_Tp' to 'bool &'
我发现以下任何更改都会使代码编译:
class A
->class A : public K
- 删除
read
专业化bool
- 删除
cv
命名空间 - 在命名空间内移动
read
模板D
不幸的是,以前的修复都不适用于我原来的问题,我仍然没有真正理解为什么它无法找到read
模板。
解决方案
推荐阅读
- qt - 仅通过链接库获取 SIGABRT
- python - 为什么在平面推导的第二个变量上会出现 UnboundLocalError?
- php - 如何从 Sigfox 服务器接收我网页上的上行链路数据?
- javascript - d3在setinterval动画完成时更改按钮样式
- sql - Teradata SQL 查询问题
- tfs - Enterprise Architect (EA) 无法识别 TFS 工作区
- matlab - 没有循环的矩阵和向量的点积
- reactjs - apollo-client 用于发出多个请求以进行实时搜索
- c++ - 如何以组合形式 C++ 打印此表?
- c# - 需要通知托盘上的按钮