Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否可以在C++;?_C++_Templates_Attributes_Typetraits - Fatal编程技术网

C++ 是否可以在C++;?

C++ 是否可以在C++;?,c++,templates,attributes,typetraits,C++,Templates,Attributes,Typetraits,我试图根据应用于函数的属性实现不同的行为。基本上,我想看看是否有可能在编译时测试属性的存在 struct AbortError { [[noreturn]] static void error(const std::string& msg) { std::cerr << "critical error: " << msg << std::endl; std::abort(); } } struc

我试图根据应用于函数的属性实现不同的行为。基本上,我想看看是否有可能在编译时测试属性的存在

struct AbortError
{
    [[noreturn]] static void error(const std::string& msg)
    {
        std::cerr << "critical error: " << msg << std::endl;
        std::abort();
    }
}

struct LogError
{
    static void error(const std::string& msg)
    {
        std::cerr << "non-critical error: " << msg << std::endl;
    }
}
struct异常
{
[[noreturn]]静态无效错误(const std::string&msg)
{
STR::CERP RP>由于C++没有,答案是“强>不<强”:不可能。

相反,我建议您做的是做出不同的设计选择,以实现相同的目标。 例如,您可以在运行时环境中使用polymorfism

如果您在编译时需要一些,您可以在结构中“集成”一个常量表达式,并在以后检查它

例如:

struct AbortError {
  static constexpr bool critical = true;
  // ...
};


一个联机示例,显示两个选项。< /p>属性不是签名的一部分,C++没有反射……即使当C++获得了反射时,它也可能不包含属性(直到稍后,如果它决定了函数包含属性的反射是一个好主意)

struct AbortError {
  static constexpr bool critical = true;
  // ...
};
struct AbortError: std::true_type {
// ...
};