如何在C ++中打印编译时计算结果? [英] How to print result of a compile-time calculation in C++?

查看:403
本文介绍了如何在C ++中打印编译时计算结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了几个constexpr函数,并在static_asserts中使用它们来控制一些资源限制。但是我希望不仅强制编译时谓词,还要查看在正常编译过程中计算的实际值,或者至少在断言失败时。



有几种方法在编译期间打印字符串消息,但关于constexpr计算的打印结果是什么? 这里是一些利用gcc诊断消息的代码在断言消息之后打印感兴趣的值。要找到感兴趣的值,您只需要搜索错误字符串 T x =

  #include< string> 

模板< class T,T x,class F>
void transparent(F f){f(); }


模板< bool B>
constexpr void my_assert(){
static_assert(B,oh no);
}

模板< int X>
void f(){
transparent< int,X + 7>([] {
transparent< long,X * X * X>([] {
my_assert< X +10 == - 89>();});});
}

int main(){
// f< 3>();
f< 4>();
// f <-99>();
}

这是它给我的错误:


g ++ h.cpp -std = c ++ 11
h.cpp:在实例化'constexpr void my_assert()[with bool B = false]':
h.cpp:16:34:需要从'f()[with int X = 4] :: __ lambda0 :: __ lambda1'
h.cpp:15:35:required from'struct f()[with int X = 4] :: __ lambda0 :: __ lambda1'
h.cpp:16:38:from'f()[with int X = 4] :: __ lambda0'
h.cpp:14:28:需要从'struct f()[with int X = 4] :: __ lambda0'
h.cpp:16:41:'void f()[with int X = 4]'
h.cpp:21:10:从这里需要
h.cpp:9:5:错误:静态断言失败:哦否
static_assert(B,不好了);
^
h.cpp:4:6:error:'void transparent(F)[with T = long int; T x = 64l ;使用局部类型'f()[with int X = 4] :: __ lambda0 :: __ lambda1'来声明F = f()[with int X = 4] :: __ lambda0 :: __ lambda1] -fpermissive]
void transparent(F f){f(); }
^
h.cpp:4:6:error:'void transparent(F)[with T = int; T x = 11 ;使用局部类型'f()[with int X = 4] :: __ lambda0'声明的F = f()[with int X = 4] :: __ lambda0]',但从未定义过[-fpermissive]



请注意粗体部分


I've wrote several constexpr functions and use them in static_asserts to control some resource limits. But I'd like to not only enforce compile-time predicate but also to see the actual values calculated during normal compilation process or at least when assertion fails.

There are ways to print string messages during compilation, but what's about printing results of constexpr computations?

解决方案

Here is some code that exploits gcc's diagnostic messages to print values of interest after an assert message. To find the values of interest, you just need to search the error string for T x =:

#include <string>

template <class T, T x, class F>
void transparent(F f) { f(); }


template <bool B>
constexpr void my_assert() { 
    static_assert(B, "oh no");
}

template <int X>
void f() {
    transparent<int, X+7>([]{
        transparent<long, X*X*X>([]{
            my_assert<X+10==-89>(); });});
}

int main() {
//    f<3>();
    f<4>();
//    f<-99>();
}

Here is the error that it got me:

g++ h.cpp -std=c++11 h.cpp: In instantiation of ‘constexpr void my_assert() [with bool B = false]’: h.cpp:16:34: required from ‘f() [with int X = 4]::__lambda0::__lambda1’ h.cpp:15:35: required from ‘struct f() [with int X = 4]::__lambda0::__lambda1’ h.cpp:16:38: required from ‘f() [with int X = 4]::__lambda0’ h.cpp:14:28: required from ‘struct f() [with int X = 4]::__lambda0’ h.cpp:16:41: required from ‘void f() [with int X = 4]’ h.cpp:21:10: required from here h.cpp:9:5: error: static assertion failed: oh no static_assert(B, "oh no"); ^ h.cpp:4:6: error: ‘void transparent(F) [with T = long int; T x = 64l; F = f() [with int X = 4]::__lambda0::__lambda1]’, declared using local type ‘f() [with int X = 4]::__lambda0::__lambda1’, is used but never defined [-fpermissive] void transparent(F f) { f(); } ^ h.cpp:4:6: error: ‘void transparent(F) [with T = int; T x = 11; F = f() [with int X = 4]::__lambda0]’, declared using local type ‘f() [with int X = 4]::__lambda0’, is used but never defined [-fpermissive]

Note that bolded parts

这篇关于如何在C ++中打印编译时计算结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆