gpt4 book ai didi

c++ - 使用模板友元函数时未解析的重载函数类型

转载 作者:太空狗 更新时间:2023-10-29 20:46:58 25 4
gpt4 key购买 nike

我在尝试将类模板的友元函数填充到 boost::function 时遇到问题:

#include <boost/function.hpp>

template <int N>
struct Vec{
friend
double dot(Vec, Vec){}
};

template class Vec<2>; //Force multiple instantiations
template class Vec<3>;

int main()
{
boost::function<double (Vec<2>, Vec<2>)> func = dot;
double (*func1)(Vec<2>, Vec<2>) = dot;
}

main 中的两行都不是会编译。对于第一个,GCC 提示:

error: conversion from '<unresolved overloaded function type>' to non-scalar type 'boost::function<double(Vec<2>, Vec<2>)>' requested

第二行的错误让我更加困惑:

error: no matches converting function 'dot' to type 'double (*)(struct Vec<2>, struct Vec<2>)'
testtmpl.C:6:15: error: candidates are: double dot(Vec<2>, Vec<2>)
testtmpl.C:6:15: error: double dot(Vec<3>, Vec<3>)

我有点受阻,因为我不明白为什么 double dot(Vec<2>, Vec<2>)不匹配。

关于这里发生的事情有什么想法吗?

最佳答案

以我的理解,如果一个友元函数定义在一个没有其他类的类中相应的声明,好友名只能通过ADL查询。
§7.3.1.2/3 说:

The name of the friend is not found by simple name lookup until a matching declaration is provided in that namespace scope

代码添加相应的就可以编译了函数声明
double dot(Vec<2>, Vec<2>);喜欢 here .

关于c++ - 使用模板友元函数时未解析的重载函数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6699946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com