gpt4 book ai didi

c++ - 在模板中使用#define VALUE

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:20 26 4
gpt4 key购买 nike

我无法将定义的值传递给模板。即使是简单的计算也行不通:

    #define IR_TIMEOUT 564*173/2
return decodeSpace<IR_TIMEOUT>(
duration);

return decodeSpace<564*173/2>(
duration);

工作:

    return decodeSpace<48786>(
duration);

定义:

    template <uint16_t irTimeout>
bool decodeSpace(unsigned long duration){}

错误:

E:\Arduino\sketchbook\libraries\IRLremote\IRLprotocolNEC.cpp: In member function 'virtual bool IRLprotocolNEC::decodeIR(long unsigned int)': E:\Arduino\sketchbook\libraries\IRLremote\IRLprotocolNEC.cpp:46:11: error: no matching function for call to 'IRLprotocolNEC::decodeSpace(long unsigned int&)' duration); ^ E:\Arduino\sketchbook\libraries\IRLremote\IRLprotocolNEC.cpp:46:11: note: candidate is: In file included from E:\Arduino\sketchbook\libraries\IRLremote/IRLprotocolNEC.h:27:0, from E:\Arduino\sketchbook\libraries\IRLremote\IRLprotocolNEC.cpp:24: E:\Arduino\sketchbook\libraries\IRLremote/CIRLremote.h:103:9: note: template bool CIRLprotocol::decodeSpace(long unsigned int) bool decodeSpace(unsigned long duration){ ^ E:\Arduino\sketchbook\libraries\IRLremote/CIRLremote.h:103:9: note: template argument deduction/substitution failed: E:\Arduino\sketchbook\libraries\IRLremote\IRLprotocolNEC.cpp:46:11: error: overflow in constant expression [-fpermissive] duration); ^ E:\Arduino\sketchbook\libraries\IRLremote\IRLprotocolNEC.cpp:46:11: note: in template argument for type 'unsigned int'

这是错误还是功能?或者更好:我怎样才能避免/解决这样的事情?

我想创建结构相同但时序不同的不同解码函数。传递值需要大量的闪存,所以我最好一遍又一遍地复制函数。这就是我想使用模板的原因。

最佳答案

问题是 564*173 溢出了 uint16。

当涉及到常量折叠时,使用定义或直接表达式是一回事。

常量折叠是编译器“展开”你的表达式的事实。例如,将“2*3”替换为“6”。

关于c++ - 在模板中使用#define VALUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25821887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com