【发布时间】:2021-02-18 16:49:29
【问题描述】:
在下面这段代码中,编译器不应该能够从概念中推断出模板参数吗?
#include <iostream>
template<typename T>
struct Bar;
template<typename F, typename T>
concept Foo = requires (F foo, Bar<T> bar)
{
{ foo.test(bar) } -> std::same_as<T>;
};
template<typename T>
struct Bar
{
T value;
template<Foo<T> F>
T test(F foo)
{
return foo.test(*this);
}
};
template<typename T>
struct FooBar
{
T value;
T test(Bar<T> bar)
{
return (value = bar.value) + bar.value;
}
};
int main(void)
{
Bar<float> b{ 1 };
std::cout << b.test(FooBar{}) << std::endl; // Fails to deduce the template argument
std::cout << b.test(FooBar<float>{}) << std::endl; // Compiles successfully
}
这是一个 MSVC 和 GCC 尚未实现的功能(两者都无法编译代码)吗?还是只是无效的 C++20 代码?如果是这样,为什么? float 不是模板参数T 唯一可能的类型吗?或者是否有一些我遗漏的替代方法可能会导致编译器产生歧义?
【问题讨论】:
-
如果您让我们知道错误消息是什么以及它发生的位置会很好。
标签: c++ templates c++20 template-argument-deduction c++-concepts