namespace FT {
enum class LD : int32_t {
B = 0,
P = 1,
N = 2
};
struct debug_test {
LD one;
string two;
int three;
debug_test(LD a, string b, int c) : one(a), two(b), three(c) {}
};
}
string toString(FT::LD const e) {
switch (e) {
case FT::LD::B: {
return std::string("bbb");
}
case FT::LD::N: {
return string("nnn");
}
case FT::LD::P: {
return string("ppp");
}
default: {
return string("default");
}
}
}
namespace FT {
//用于将LD信息转化成string的信息后可以输入给输出信息
inline std::ostream &operator<<(std::ostream &os, LD const &ld) {
return os << ::toString(ld);
}
//将全部内容都转化成一个stringstream流,这样子可以输出
inline std::ostream &operator<<(std::ostream &os, debug_test const &dt) {
os << "LD(";
os << dt.one;
os << ")"<<std::endl;
os << "string(";
os << dt.two;
os << ")"<<std::endl;
os << "int(";
os << dt.three;
os << ")";
}
}
namespace std {
inline std::string to_string(FT::LD const &ld) {
return toString(ld);
}
inline std::string to_string(FT::debug_test const &dt) {
stringstream sstream;
sstream << dt;
return sstream.str();
}
}
struct AA {
typedef std::shared_ptr<AA> Ptr;
int a;
AA(int _a) : a(_a) {}
};
//主函数,可以自己定义在std中的to_string函数,只要输入的内容是标准库中没有的就可以了,一般自己定义的结构体标准库中也不会有的
int main() {
//debug自己信息
FT::debug_test dt(FT::LD::P, "hello", 0);
cout << std::to_string(dt) << endl;
//使用自己定义的智能指针类型
AA::Ptr A_ptr(new AA(5));
cout << A_ptr->a << endl;
return 0;
}
使用#if的方式进行调试和注释最简单的例子
#if condition
code1
#else
code2
#endif
例子
#if 1
cout<<"debug"<<endl;
#else
cout<<"real"<<endl;
#endif
可以首先将condition写成1,在code1写需要调试的代码,在调试完毕后,将condition变成0,就可以使用code2中的代码
在自己的代码中重载标准库中的内容的时候的写法,需要返回类的绝对值的时候其实是部分的绝对值,返回的是有这种绝对值的内容的对象,所以整体的写法是
namespace FT {
class aaa {
public:
explicit aaa(double const maa_) : maa(maa_) {}
explicit operator double () const
{
return maa;
}
private:
double maa;
};
}
namespace std{
inline ::FT::aaa fabs(const ::FT::aaa other){
::FT::aaa const result(std::fabs(static_cast<double >(other)));
return result;
}
}