我已经阅读了为什么不专业化功能模板,经过一些实验,我发现了一件有趣的事情.这里去main.cxx:
// main.cxx
#include <iostream>
// Declarations
/*
template<class T>
void foo(T);
template<>
void foo(int*);
template<class T>
void foo(T*);
*/
// Definition and specification
template<class T>
void foo(T x)
{
std::cout << "T version." << std::endl;
}
template<>
void foo(int *i)
{
std::cout << "int* version." << std::endl;
}
template<class T>
void foo(T *x)
{
std::cout << "T* version" << std::endl;
}
int main(int argc, char** argv)
{
int *p;
foo(p);
}
Run Code Online (Sandbox Code Playgroud)
有趣的是:如果我留下声明部分进行评论,那么行为就像文章所说的那样,即如果int*version的定义在其定义之前,副本和经文之前,将使用T*版本.但是,如果取消注释声明块,则无论我在定义或声明中使用哪个顺序,都只会调用int*version.我的问题是这个声明如何影响决议?
有任何想法吗?我在x86_64-redhat-linux上使用g ++ 4.2.2
编辑:看到AProgrammer的答案后简化这个问题
如果在交互模式下使用Tcl,我在其中输入以下内容:
set list {1 2 3 4 5}
set sum 0
foreach el $list {
set sum [expr $sum + $element]
}
Run Code Online (Sandbox Code Playgroud)
它会显示一条非常简洁的信息:
can't read "element": no such variable
Run Code Online (Sandbox Code Playgroud)
但是当我用的时候
puts $errorInfo
Run Code Online (Sandbox Code Playgroud)
它将显示:
can't read "element": no such variable
while executing
"expr $sum + $element"
("foreach" body line 2)
invoked from within
"foreach el $list {
set sum [expr $sum + $element]
}"
Run Code Online (Sandbox Code Playgroud)
这就是我真正想要的.
问题是:在非交互模式下,当我想捕获此错误然后放入errorInfo以获得堆栈跟踪时,它只会显示简洁信息.如何获得如上所述的详细堆栈跟踪?非常感谢!
编辑添加更多细节
说我有以下代码:
proc test1 {} {
set list {1 2 3 4 5}
set sum …Run Code Online (Sandbox Code Playgroud)