简体   繁体   English

调用 constexpr 函数的给定重载时触发编译时错误

[英]Trigger compile time error when given overload of the constexpr function is called

I am writing custom variadic template that performs lookup in compile-time map.我正在编写在编译时映射中执行查找的自定义可变参数模板。

The problem is, I want to trigger compile-time error each time the value is not found in that map - preferably with descriptive error message.问题是,每次在该映射中找不到该值时,我都想触发编译时错误 - 最好带有描述性错误消息。

example code:示例代码:

template<key_t k, class pair, class... pairs>
static constexpr value_t get_local(std::tuple<pair, pairs...>)
{
    return (pair::key == k) ? pair::value : get_local<k>(std::tuple<pairs...>{});
}

template<key_t k> static constexpr value_t get_local(std::tuple<>)
{
    // Trigger error!
}

I can leave get_local undefined in the second part of code, and it actually triggers linker error, but that is not falling into category of "descriptive error message".我可以在代码的第二部分中保留get_local未定义,它实际上会触发链接器错误,但这不属于“描述性错误消息”的类别。

Static asserts, I believe, would be of no use here.我相信静态断言在这里没有用处。

I am using C++ 17我正在使用 C++ 17

And immediate drop in solution is to delete the overload:立即删除解决方案是delete重载:

template<key_t k> static constexpr value_t get_local(std::tuple<>) = delete;

This will provide the somewhat descriptive message that we try to use a base case that doesn't exist.这将提供一些描述性信息,即我们尝试使用不存在的基本情况。

Alternatively, with a static assertions in place:或者,使用静态断言:

template<key_t k> struct always_false { static constexpr bool value = false; };

template<key_t k> static constexpr value_t get_local(std::tuple<>)
{
    static_assert(always_false<k>::value, "Hit bad case!");
    return std::declval<value_t>();
}

The always_false utility is needed to make the assertion condition dependent, so the template is not ill-formed;需要使用always_false实用程序来使断言条件依赖,因此模板不会格式错误; no diagnostic required, as static_assert(false, ...) would make it.不需要诊断,因为static_assert(false, ...)可以做到。

Mind you, that you instantiate this overload yourself in your own conditional operator:请注意,您自己在自己的条件运算符中实例化了这个重载:

(pair::key == k) ? pair::value : get_local<k>(std::tuple<pairs...>{});

When you hit the case of a tuple with a single element.当您遇到具有单个元素的元组时。 Both "branches" of the conditional expression must be valid.条件表达式的两个“分支”都必须有效。 Best handle it conditionally with an if constepxr :最好使用if constepxr有条件地处理它:

if constexpr (pair::key == k) return pair::value;
else                          return get_local<k>(std::tuple<pairs...>{});

Since you did indicate pair::key == k can be evaluated in a constant expression.由于您确实表示可以在常量表达式中计算pair::key == k

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果调用 function 的特定重载,如何引发编译时错误? - How to provoke a compile-time error if a specific overload of a function is called? 是否有可能确保在编译时最多调用一次constexpr函数? - Is it possible to ensure a constexpr function is called at most once at compile time? 当在编译时未知参数时,不会调用constexpr构造函数 - `constexpr` constructor is not called when arguments are not known at compile time 什么时候在编译时评估 constexpr? - When is a constexpr evaluated at compile time? 什么时候constexpr函数在编译时得到评估? - When does a constexpr function get evaluated at compile time? 如果没有 if-constexpr 成功,触发编译时错误的最佳方法? - Best way to trigger a compile-time error if no if-constexpr's succeed? 传递constexpr函数以在编译时使用 - Passing a constexpr function to use at compile time 是否可以测试是否在编译时评估了 constexpr 函数? - Is it possible to test if a constexpr function is evaluated at compile time? constexpr函数在编译时不计算值 - constexpr function not calculate value in compile time 是什么阻止了这个 constexpr function 的编译时评估? - What is preventing compile time evaluation of this constexpr function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM