gpt4 book ai didi

c++ - 是可调用和模糊调用 : bug in either g++ or clang

转载 作者:可可西里 更新时间:2023-11-01 17:58:11 25 4
gpt4 key购买 nike

考虑以下代码:

// Preamble
#include <iostream>
#include <type_traits>

// A base class
template <class T>
struct base {void operator()(T){};};

// Two derived classes inheriting from the same base classes
template <class... T>
struct derived1: base<T>... {using base<T>::operator()...;};
template <class... T>
struct derived2: base<T>... {using base<T>::operator()...;};

// A class inheriting from both derived1 and derived2
template <class T0, class... T>
struct functor: derived1<T0>, derived2<T0, T...> {
using derived1<T0>::operator();
using derived2<T0, T...>::operator();
};

// Main function
int main() {
std::cout << std::is_invocable_v<functor<int, float, char>, int> << "\n";
std::cout << std::is_invocable_v<functor<int, float, char>, float> << "\n";
std::cout << std::is_invocable_v<functor<int, float, char>, char> << "\n";
return 0;
}

调用functor<int, float, char>::operator()(int)是不明确的,因为这个运算符被继承了两次,来自 derived1derived2 (假设为了复杂的 SFINAE 目的,我希望它是模棱两可的)。

关于 clang++-5.0 ,代码的输出是0 , 1 , 1 , 而在 g++-7.2输出是 1 , 1 , 1 .哪一个是对的?是否有解决方法,创建一个新的 struct is_unambiguously_invocable在等待错误修复时?

最佳答案

你的推理是正确的。请注意,gcc 正确地禁止调用本身:

functor<int, float, char>()(42); // error: base<int> is an ambiguous base

它只是错误地检测到此调用格式错误。报告为 gcc bug 84869 . T.C.在没有库依赖性的错误报告中添加了进一步减少的复制:

struct base {
void operator()(int ) { }
};

struct a : base { };
struct b : base { };

struct f: a, b {
using a::operator();
using b::operator();
};

template<class T> auto g(int) -> decltype(T()(0), 0);
template<class T> auto g(...) -> long;

template<class, class> struct Same;
template<class T> struct Same<T, T> {};

Same<decltype(g<f>(0)), long> s; // should be okay, but gcc errors because it
// thinks decltype(g<f>(0)) is int

关于c++ - 是可调用和模糊调用 : bug in either g++ or clang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49084661/

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