是否可以static_assert lambda 不是通用的

Is it possible to static_assert that a lambda is not generic?

本文关键字:lambda assert static 是否      更新时间:2023-10-16

我实现了一个访问函数(在变体上(,该函数检查变体中当前活动类型是否与函数签名(更准确地说是第一个参数(匹配。基于这个不错的答案。例如

#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 可能会调用类型的任意函数。换句话说,我不能尝试在 intstd::string 的 2 std::void_t测试中调用 lambda,如果它有效,则假设它是通用的,因为他们可能会尝试在 intstring 上调用 .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。

我认为不可能对模板功能做同样的事情,但不确定。