gpt4 book ai didi

C++__FILE__宏的类型是什么

转载 作者:IT老高 更新时间:2023-10-28 23:12:31 26 4
gpt4 key购买 nike

我正在尝试创建一个异常类。为此,我重载了 <<运算符(operator)。所以代码是这样的

class RunAndCheck
{
opearator << (boost::any given)
{

//Here goes the value of the "given"

}
};

用法是这样的

RunAndCheck s;
s << file->open() << __FILE__ << __LINE__ ;

所以问题是我想知道FILE的类型,那么只有我可以从boost::any中提取字符串.任何人都可以激发您对此的好奇心吗?

最佳答案

__FILE__ 扩展为字符串文字,就像您直接编写“/path/to/current/file.cpp”一样。字符串文字是不可修改的字符数组左值。

你想要模板化 << 而不是使用 boost::any:

class RunAndCheck {
public:
template<class T>
RunAndCheck& operator<<(const T& value) {
// ...
return *this;
}
};

或者您想为所有可接受的类型提供重载:

class RunAndCheck {
public:
RunAndCheck& operator<<(const char* value) {
// ...
return *this;
}
RunAndCheck& operator<<(int value) {
// ...
return *this;
}
};

关于C++__FILE__宏的类型是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5065091/

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