如何在现代C ++中没有宏的情况下实现系统特定的功能

问题描述 投票:0回答:1

JetBrains C ++的ReSharper告诉我更换类似的东西

#ifdef _WIN32
#    define cls system("cls")
#else // Assuming Unix
#    define cls system("tput clear")
#endif // _WIN32

具有constexpr模板功能。

但是,我尝试通过std::enable_if_t<_WIN32>使用SFINAE,但出现错误,提示“不能重载仅由返回类型区分的函数”(诚然,我没有使用模板函数,而是使用enable_if返回类型)。

除了使用enable_if作为返回类型之外,我不知道如何使用constexpr模板函数来实现预处理器的工作。

从更一般的意义上讲,我希望能够基于不依赖于其他模板参数的编译时布尔值来启用函数重载。

提前感谢!

c++ templates sfinae preprocessor compile-time
1个回答
0
投票

您不想要enable_if,这是在您可能需要根据类型参数等在编译时做出决策的情况下。

预处理器在这里是适当的,尽管使用普通函数比使用宏更干净。

#ifdef _WIN32
void cls() { system("cls"); }
#else // Assuming Unix
void cls() { system("tput clear"); }
#endif // _WIN32
© www.soinside.com 2019 - 2024. All rights reserved.