是否可以static_assert lambda 不是通用的
Is it possible to static_assert that a lambda is not generic?
我实现了一个访问函数(在变体上(,该函数检查变体中当前活动类型是否与函数签名(更准确地说是第一个参数(匹配。基于这个不错的答案。例如
#include <variant>
#include <string>
#include <iostream>
template<typename Ret, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(*) (Arg, Rest...));
template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...));
template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...) const);
template <typename F>
decltype(first_argument_helper(&F::operator())) first_argument_helper(F);
template <typename T>
using first_argument = decltype(first_argument_helper(std::declval<T>()));
std::variant<int, std::string> data="abc";
template <typename V>
void Visit(V v){
using Arg1 = typename std::remove_const_t<std::remove_reference_t<first_argument<V>>>;//... TMP magic to get 1st argument of visitor + remove cvr, see Q 43526647
if (! std::holds_alternative<Arg1>(data)) {
std::cerr<< "alternative mismatchn";
return;
}
v(std::get<Arg1>(data));
}
int main(){
Visit([](const int& i){std::cout << i << "n"; });
Visit([](const std::string& s){std::cout << s << "n"; });
// Visit([](auto& x){}); ugly kabooom
}
这有效,但是当用户传递泛型(例如 [](auto&){}
( λ。有没有办法检测到这一点并给出很好的static_assert()
?如果它也适用于函数模板,而不仅仅是 lambda,那也很好。
请注意,我不知道可能的 lambda 做了什么,所以我不能对 Dummy 类型做一些聪明的事情,因为 lambda 可能会调用类型的任意函数。换句话说,我不能尝试在 int
和 std::string
的 2 std::void_t
测试中调用 lambda,如果它有效,则假设它是通用的,因为他们可能会尝试在 int
和 string
上调用 .BlaLol()
。
有没有办法检测到这一点并给出很好的static_assert?
我想你可以使用SFINAE而不是operator()
类型。
遵循示例
#include <type_traits>
template <typename T>
constexpr auto foo (T const &)
-> decltype( &T::operator(), bool{} )
{ return true; }
constexpr bool foo (...)
{ return false; }
int main()
{
auto l1 = [](int){ return 0; };
auto l2 = [](auto){ return 0; };
static_assert( foo(l1), "!" );
static_assert( ! foo(l2), "!" );
}
如果您想通过 decltype()
使用它,您可以返回 std::true_type
(从第一个版本foo()
(或std::false_type
(从第二个版本(而不是bool
。
我认为不可能以如果它也适用于函数模板,而不仅仅是 lambda,那也很好。
如此简单的方式:lambda(也是通用lambda(是一个对象;模板函数不是一个对象,而是一组对象。您可以将对象传递给一个函数,而不是一组对象。
但是前面的解决方案也应该适用于具有operator()
s的类/结构:当有一个单一的非模板时,operator()
,你应该从foo()
获得1
;否则(没有operator()
,多个operator()
,模板operator()
(,foo()
应该返回0
。
另一个更简单的选择:
#include <type_traits>
...
template <typename V>
void Visit(V v) {
class Auto {};
static_assert(!std::is_invocable<V, Auto&>::value);
static_assert(!std::is_invocable<V, Auto*>::value);
...
}
Auto
类只是一种发明的类型,不可能出现在V
参数中。如果V
接受Auto
作为参数,它必须是泛型。
我在coliru中进行了测试,我可以确认解决方案涵盖以下情况:
Visit([](auto x){}); // nice static assert
Visit([](auto *x){}); // nice static assert
Visit([](auto &x){}); // nice static assert
Visit([](auto &&x){}); // nice static assert
我不确定这是否会涵盖您不知道哪些是:)的所有可能的 lambda
#include <variant>
#include <string>
#include <iostream>
template <class U, typename T = void>
struct can_be_checked : public std::false_type {};
template <typename U>
struct can_be_checked<U, std::enable_if_t< std::is_function<U>::value > > : public std::true_type{};
template <typename U>
struct can_be_checked<U, std::void_t<decltype(&U::operator())>> : public std::true_type{};
template<typename Ret, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(*) (Arg, Rest...));
template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...));
template<typename Ret, typename F, typename Arg, typename... Rest>
Arg first_argument_helper(Ret(F::*) (Arg, Rest...) const);
template <typename F>
decltype(first_argument_helper(&F::operator())) first_argument_helper(F);
template <typename T>
using first_argument = decltype(first_argument_helper(std::declval<T>()));
std::variant<int, std::string> data="abc";
template <typename V>
void Visit(V v){
if constexpr ( can_be_checked<std::remove_pointer_t<decltype(v)>>::value )
{
using Arg1 = typename std::remove_const_t<std::remove_reference_t<first_argument<V>>>;//... TMP magic to get 1st argument of visitor + remove cvr, see Q 43526647
if (! std::holds_alternative<Arg1>(data))
{
std::cerr<< "alternative mismatchn";
return;
}
v(std::get<Arg1>(data));
}
else
{
std::cout << "it's a template / auto lambda " << std::endl;
}
}
template <class T>
void foo(const T& t)
{
std::cout <<t << " foo n";
}
void fooi(const int& t)
{
std::cout <<t << " fooi " << std::endl;
}
int main(){
Visit([](const int& i){std::cout << i << std::endl; });
Visit([](const std::string& s){std::cout << s << std::endl; });
Visit([](auto& x){std::cout <<x << std::endl;}); // it's a template / auto lambda*/
Visit(foo<int>);
Visit<decltype(fooi)>(fooi);
Visit(fooi);
// Visit(foo); // => fail ugly
}
我不知道这是否是你想要的,但如果自动 lambda 作为参数传递,你可以用这个static_assert。
我认为不可能对模板功能做同样的事情,但不确定。
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 可组合的lambda/std::函数与std::可选
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 如何将lambda作为模板类的成员函数参数
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 在 lambda 捕获中声明的变量的类型推导
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 模板函数指针和lambda
- 两组使用lambda函数的大括号
- 使lambda不可复制/不可移动
- FLTK:按下哪个按钮 - 将数字传递给按钮的回调 (lambda)
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- AWS Lambda C++运行时权限被拒绝
- 捕获lambda中的std::数组
- 这 4 个 lambda 表达式之间有什么区别?
- 在实现文件中使用头文件的通用 lambda
- 在 Xcode [c++] 中在断言 (assert.h) 中定义 lambda 时,为类似函数的宏调用提供过多参数会导