为什么你不能在这里传递文字字符串?我使用了一个非常轻微的解决方法.
template<const char* ptr> struct lols {
lols() : i(ptr) {}
std::string i;
};
class file {
public:
static const char arg[];
};
decltype(file::arg) file::arg = __FILE__;
// Getting the right type declaration for this was irritating, so I C++0xed it.
int main() {
// lols<__FILE__> hi;
// Error: A template argument may not reference a non-external entity
lols<file::arg> hi; // Perfectly legal
std::cout << hi.i;
std::cin.ignore();
std::cin.get();
}
Run Code Online (Sandbox Code Playgroud) 我想创建包含info(),debug()和error()函数的log.c文件.这些功能无需打印文件名,行号等.因此,当我调用此函数之一时,我想转储调用者的文件名,行号和函数名.那么我们如何准确追溯?有没有办法追溯C,或者,如果我们使用宏,这怎么办?
我在不同的源文件中有两个函数:
a.cpp
void A()
{
B();
}
Run Code Online (Sandbox Code Playgroud)
b.cpp
void B()
{
std::cout << "B() called from file: " << ??? << " line: " << ??? << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如何获取呼叫的文件名和行号?
可能重复:
C/C++行号
我想显示触发printf的行号?
它可能看起来像这样:
printf("the line number is: %d",SOME_LIBC_MACRO);
Run Code Online (Sandbox Code Playgroud)
怎么做?
可能重复:
C/C++行号
嗨,
我有一个简单的错误管理器类,其他类用于报告错误,然后将错误打印到日志文件中供以后检查.我可以打印出描述并给出错误代码.我还想要的是,它记录错误记录的文件名和行号(自动,而不是每次都写它).有办法吗?我知道它已经完成,因为我已经看到它,我只是找不到解决方案 - 可能是由于不正确的搜索条件.
谢谢!