首页 > 解决方案 > 是否可以为不应编译的表达式表达 static_assert ?

问题描述

我想用以下形式表达一个 static_assert:

static_assert(expression should not compile);

让我添加一个完整的示例:

template <bool Big>
struct A{};

template <>
struct A<true>
{
    void a() {}
};

A<false> b;

static_assert(!compile(b.a()));
or
static_assert(!compile(A<false>::a()));

因此,想法是能够确保表达式(具有有效语法)不会编译。

如果可能的话,解决方案只使用 C++11 会更好。

标签: c++c++11static-assert

解决方案


好的,鉴于您的问题的上下文有些模糊,这个答案可能不适合您的情况。然而,我发现这是一个非常有趣的挑战。

显然,正如评论中所述,解决方案将不得不利用某种(表达式)SFINAE。基本上,我们需要的是检测习语的更通用的变体。但是,这里主要有两个问题:

1) 要让 SFINAE 发挥作用,我们需要某种模板。

2) 为了提供compile(XXX)语法,我们需要在宏中“即时”创建这些模板。否则我们必须提前为每个测试定义一个测试函数。

第二个约束使事情变得相当困难。我们可以在 lambdas 中本地定义结构和函数。不幸的是,那里不允许使用模板。

所以这里是,我能走多远(不是 100% 你想要的,但相对接近)。

通常,表达式-SFINAE-检测器利用(模板)函数重载或类模板特化。由于两者都不允许在 lambda 中使用,我们需要一个额外的层:一个函子,它接受一堆 lambda 并调用最适合调用参数的那个。这通常与 结合使用std::variant

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; 
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; 

现在我们可以像这样创建一个检测器:

auto detector =    overloaded{
     [](auto, auto) -> std::false_type {return {};}
    ,
    [](auto x, int)-> decltype(decltype(x)::a(), std::true_type{}){ return {};}
    };

static_assert(!detector(A<false>{}, int{}));
static_assert(detector(A<true>{}, int{}));

现在,我们可以定义一个宏,它为所需的表达式定义并调用一个检测器:

#define compile(obj, xpr)                                                   \
  []() {                                                                    \
    auto check =                                                            \
        overloaded{[](auto&&, auto) -> std::false_type { return {}; },      \
                   [](auto&& x, int) -> decltype(x xpr, std::true_type{}) { \
                     return {};                                             \
                   }};                                                      \
    return decltype(check(obj, int{})){};                                   \
  }()

该宏创建一个 lambda,将 替换xpr到检测器中,并执行类型推导decltype(x)以使 SFINAE 生效。它可以按如下方式使用:

static_assert(!compile(b, .a()));
static_assert(compile(a, .a()));

int x = 0;
static_assert(compile(x, *= 5));
static_assert(!compile(x, *= "blah"));

不幸的是,它不能使用类型名作为第一个参数。因此,我们需要第二个宏来处理这些类型的 af 测试:

#define compile_static(clazz, xpr)                                       \
  []() {                                                                 \
    auto check = overloaded{                                             \
        [](auto, auto) -> std::false_type { return {}; },                \
        [](auto x, int) -> decltype(decltype(x) xpr, std::true_type{}) { \
          return {};                                                     \
        }};                                                              \
    return decltype(check(std::declval<clazz>(), int{})){};              \
  }()

static_assert(!compile_static(A<false>, ::a()));
static_assert(compile_static(A<true>, ::a()));

如上所述,这不是您要求的 100%,因为我们总是需要额外,的来分隔宏参数。此外,它需要两个单独的宏。也许这是可以使用预处理器来检测xpr参数是否以::. 当然,在某些情况下,它可能不起作用。但也许这是一个起点。

它需要 c++17,但似乎适用于 gcc >= 7、clang >= 5 甚至 msvc 19。

这是一个完整的例子


推荐阅读