C ++检查if语句是否可以被评估为constexpr

问题描述 投票:19回答:3

有没有一种方法来决定是否可以对constexpr进行评估,并将结果用作constexpr布尔值?我简化的用例如下:

template <typename base>
class derived
{
    template<size_t size>
    void do_stuff() { (...) }

    void do_stuff(size_t size) { (...) }
public:
    void execute()
    {
        if constexpr(is_constexpr(base::get_data())
        {
            do_stuff<base::get_data()>();
        }
        else
        {
            do_stuff(base::get_data());
        }
    }
}

我的目标是C ++ 2a。

我发现了以下reddit线程,但我不是宏的忠实粉丝。 https://www.reddit.com/r/cpp/comments/7c208c/is_constexpr_a_macro_that_check_if_an_expression/

c++ template-meta-programming constexpr c++20 if-constexpr
3个回答
17
投票

这是另一个解决方案,它更通用(适用于任何表达式,每次都不定义单独的模板)。

这个解决方案利用了(1)lambda表达式可以是constexpr,因为C ++ 17(2)无捕获lambda的类型是C ++ 20的默认构造。

这个想法是,当且仅当true出现在模板参数中时才会选择返回Lambda{}()的重载,这有效地要求lambda调用是一个常量表达式。

template<class Lambda, int=(Lambda{}(), 0)>
constexpr bool is_constexpr(Lambda) { return true; }
constexpr bool is_constexpr(...) { return false; }

template <typename base>
class derived
{
    // ...

    void execute()
    {
        if constexpr(is_constexpr([]{ base::get_data(); }))
            do_stuff<base::get_data()>();
        else
            do_stuff(base::get_data());
    }
}

11
投票

不完全是你问的问题(我开发了一个特定于get_value()静态方法的自定义类型特征......也许它可以概括它,但目前,我不知道如何)但我想你可以使用SFINAE和做出如下的事情

#include <iostream>
#include <type_traits>

template <typename T>
constexpr auto icee_helper (int)
   -> decltype( std::integral_constant<decltype(T::get_data()), T::get_data()>{},
                std::true_type{} );

template <typename>
constexpr auto icee_helper (long)
   -> std::false_type;

template <typename T>
using isConstExprEval = decltype(icee_helper<T>(0));

template <typename base>
struct derived
 {
   template <std::size_t I>
   void do_stuff()
    { std::cout << "constexpr case (" << I << ')' << std::endl; }

   void do_stuff (std::size_t i)
    { std::cout << "not constexpr case (" << i << ')' << std::endl; }

   void execute ()
    {
      if constexpr ( isConstExprEval<base>::value )
         do_stuff<base::get_data()>();
      else
         do_stuff(base::get_data());
    }
 };

struct foo
 { static constexpr std::size_t get_data () { return 1u; } };

struct bar
 { static std::size_t get_data () { return 2u; } };

int main ()
 { 
   derived<foo>{}.execute(); // print "constexpr case (1)"
   derived<bar>{}.execute(); // print "not constexpr case (2)"
 }

7
投票
template<auto> struct require_constant;
template<class T>
concept has_constexpr_data = requires { typename require_constant<T::get_data()>; };

这基本上是std::ranges::split_view使用的。

© www.soinside.com 2019 - 2024. All rights reserved.