c++ - Overload resolution of template methods with ref-qualifiers
问题描述
I'm developing a container with compile-time access function using special type. I also want to have an access function using a number in order to implement ops for all elements. Thus I have something like this:
struct S
{
template<int I> int& f();
template<class Q> int& f();
};
I want to forbid access for temporary objects, so I add an overload for type-access:
struct S
{
template<int I> int& f();
template<class Q> int& f() &;
template<class Q> int& f() && = delete;
};
But then I have a problem with msvc compiler:
<source>(4): error C2560: 'int &Test::f(void) &': cannot overload a member function with ref-qualifier with a member function without ref-qualifier
However both gcc and clang accept it. Who is right?
解决方案
MSVC is wrong here.
The relevant rule is [over.load]/2.3:
Member function declarations with the same name and the same parameter-type-list as well as member function template declarations with the same name, the same parameter-type-list, and the same template parameter lists cannot be overloaded if any of them, but not all, have a ref-qualifier ([dcl.fct]).
Here the function templates have different template parameters (int I
and class Q
), so this rule does not apply, and there is no other rule stoping them from overloading.
推荐阅读
- javascript - 如果访问状态,React TypeScript 对象将变为未定义
- python - 暂停循环并等待用户按键每'n'次迭代。- Python
- javascript - 我怎样才能循环这个数组?
- typescript - 函数返回未定义的期望 Promise 或使用 async 和 await 的值
- babeljs - 有没有办法设置 Babel 以不断地将 src 文件夹转换和/或缩小到已编译的文件夹?
- sql - 从 2 个表中提取数据的 SQL 查询
- node.js - 循环合并 PDF 文件 ( Hummus-Recipe )
- java - docker compose 无法连接到 postgresql
- c# - 需要一种不使用正则表达式来提取标题内容的方法
- java - 在 Java 8 中等待任何未来,而不为每个未来创建线程