繁体   English   中英

如果compile-time-constant参数错误,则生成编译时错误

[英]Generate compile-time error if compile-time-constant parameter is wrong

我正在尝试编写一个函数,如果使用编译时常量参数调用它将触发编译时错误,如果参数的值与static_assert不匹配,但仍然可以在运行时使用计算机调用值。

有点像这样:

template<int N> void f(N){
  static_assert(N == 5, "N can only be 5.");
  do_something_with(N);
}

void f(int N){
  if(N == 5){
    do_something_with(N);
  }
}

volatile int five = 5;
volatile int six = 6;

int main() {  
  f(5); //ok
  f(6); //compile-time error
  f(five); //ok
  f(six); //run-time abort

  return 0;
}

我怎样才能做到这一点?

此外,如果可能的话,我希望能够保留简单的f(something)语法,因为这段代码适用于不熟悉模板语法的初学者程序员可以使用的库。

我能想象的最好的是一个抛出异常的constexpr函数。

如果在编译时执行,则throw会导致编译错误; 如果在运行时执行,则抛出异常

有点喜欢

#include <stdexcept>

constexpr int checkGreaterThanZero (int val)
 { return val > 0 ? val : throw std::domain_error("!"); }

int main()
 {
   // constexpr int ic { checkGreaterThanZero(-1) }; // compile error

   int ir { checkGreaterThanZero(-1) }; // runtime error
 }

- 编辑 -

正如yuri kilocheck所指出的,你可以调用std::abort() ;而不是抛出异常。 通过例子

constexpr int checkGreaterThanZero (int val)
 { return val > 0 ? val : (std::abort(), 0); }

使用不同的语法,您可以:

template <int N>
using int_c = std::integral_constant<int, N>;

namespace detail {
    template <std::size_t N>
    constexpr int to_number(const int (&a)[N])
    {
        int res = 0;
        for (auto e : a) {
            res *= 10;
            res += e;
        }
        return res;
    }
}

template <char ... Ns>
constexpr int_c<detail::to_number({(Ns - '0')...})> operator ""_c ()
{
    return {};
}

#if 1 // Write this way
// Compile time
template <int N> void f(int_c<N>) = delete;
void f(int_c<5>) { do_something_with(5); }

#else // Or like this
// Compile time
template <int N>
void f(int_c<N>)
{
    static_assert(N == 5, "!");
    do_something_with(N);
}

#endif

// Runtime
void f(int N){
    if (N == 5) {
        std::abort(); // Or any other error handling
    }
    f(5_c);
}

int main(){
    f(5_c); // ok
    // f(6_c); // Won't compile
    f(5); // ok
    f(6); // abort at runtime
}

演示

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM