【发布时间】:2021-08-10 18:12:37
【问题描述】:
我想在 C++11 的编译时检查两个 POD 类型是否相等。我正在尝试 #define 适合 Float_T 类型的函数名称。这是我尝试过的:
#include <type_traits>
using Float_T = double;
// using Float_T = float;
#if is_same<Float_T, double>::value
#define LOG2 log2
#else
#define LOG2 log2f
#endif
g++ complains: error: extra text after expected end of preprocessing directive
#if is_same<Float_T, double>::value
^
谁能建议一种方法来做到这一点?
【问题讨论】:
-
你的意思是c++11吗?
-
C 还是 C++?编译时常量在编译时使用,而不是解析时使用。
-
顺便说一句,如果你使用
std::log2,函数已经重载,所以这个技巧没用。 -
您不能使用 STL 功能 (is_same) 作为预处理器宏的条件。 C 预处理器对 STL 一无所知,也无法评估它。况且整个sn-p看起来像是徒劳的运动,闻着XY问题。
-
有很多方法可以做这样的事情。您要解决的实际问题是什么?
标签: c++ c++11 c-preprocessor typetraits