【发布时间】:2022-01-24 02:30:00
【问题描述】:
我有一个概念可以帮助我检测函数的签名:
template <typename>
struct FuncHelper;
template <typename R, typename... Args>
struct FuncHelper<R(Args...)> {
template <typename Func>
static constexpr bool is_like = requires(const Func& f, Args... args) {
{ f(std::forward<Args>(args)...) } -> std::convertible_to<R>;
};
};
template <typename FX, typename T>
concept function_like = FuncHelper<FX>::template is_like<T>;
我可以在函数重载中使用这个概念作为约束:
template <typename T> requires function_like<bool(), T>
void testWorks(T&& func) { }
// I can call this like testWorks([&](){ return true; });
但是,如果我在模板参数中指定此约束:
template <function_like<bool()> T>
void testFails(T&& func) { }
// testFails([&](){ return true; }); // Compiler error: incomplete type
然后我收到一个编译器错误,提示我的类型不完整。
这里是完整的代码:
#include <concepts>
#include <type_traits>
#include <utility>
using namespace std;
template <typename>
struct FuncHelper;
template <typename R, typename... Args>
struct FuncHelper<R(Args...)> {
template <typename Func>
static constexpr bool is_like = requires(const Func& f, Args... args) {
{ f(std::forward<Args>(args)...) } -> std::convertible_to<R>;
};
};
template <typename FX, typename T>
concept function_like = FuncHelper<FX>::template is_like<T>;
template <typename T> requires function_like<bool(), T>
void testWorks(T&& func) { }
template <function_like<bool()> T>
void testFails(T&& func) { }
void testAll() {
testWorks([&](){ return true; }); // No errors!
testFails([&](){ return true; }); // Compiler error: incomplete type
}
你可以自己试试:
GCC:https://godbolt.org/z/fG6c7E3qf
Clang:https://godbolt.org/z/7G7T8nTde
我认为testWorks 和testFails 都应该做同样的事情。我哪里错了?
【问题讨论】:
标签: c++ templates lambda constraints c++20