是否可以为不应编译的表达式表达static_assert?

LeD*_*YoM 8 c++ static-assert c++11

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

static_assert(expression should not compile);
Run Code Online (Sandbox Code Playgroud)

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

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()));
Run Code Online (Sandbox Code Playgroud)

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

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

flo*_*tan 3

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

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

1) 为了让 SFINAE 启动,我们需要某种模板。

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

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

所以这就是我能够达到的程度(不是 100% 你想要的,但相对接近)。

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

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; 
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; 
Run Code Online (Sandbox Code Playgroud)

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

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{}));
Run Code Online (Sandbox Code Playgroud)

现在,我们可以定义一个宏,它定义并调用所需表达式的向量:

#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{})){};                                   \
  }()
Run Code Online (Sandbox Code Playgroud)

该宏创建一个 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"));
Run Code Online (Sandbox Code Playgroud)

不幸的是,它不适用于类型名作为第一个参数。因此,我们需要第二个宏来进行这些类型的 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()));
Run Code Online (Sandbox Code Playgroud)

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

它需要 c++17,但似乎可以与 gcc >= 7、clang >= 5 甚至 msvc 19 配合使用。

这是一个完整的例子