楼主:
saladim (杀拉顶)
2019-07-02 01:28:24大家好, 最近刚好翻到the C++ programming language,章节C.13.8.4
有关templates, namespace 跟 specialization的, 看不太懂他所要表达的意思.
首先呢, 上面提到:
.......省略.......
This reflects three obvious strategies an implementation can use for
generating specializations:
[1] Generate a specialization the first time a call is seen.
[2] At the end of a translation unit, generate all specializations needed for
that translation unit.
[3] Once every translation unit of a program has been seen, generate all
specializations needed for the program.
All three strategies have strengths and weaknesses, and combinations of these
strategies are also possible.
......省略.......
然后说:
A program is illegal, if it is possible to construct two different meanings
by choosing different points of instantiation or different contents of
namespaces at different possible contexts for generating the specialization.
然后有一段范例码:
namespace N
{
class A{ /* ... */ };
char f(A,int) ;
}
template<class T, class T2> char g(T t, T2 t2) { return f(t,t2) ; }
//error (alternative resolutions of f(t))
char c= g(N: :A() ,'a') ; //<
作者:
CoNsTaR ((const *))
2019-07-03 07:45:00欸... 不论用哪种策略都不会影响编译成功与否吧,只是有各自的优缺点(他这样讲我猜是最佳化难易度之类的编译时期才有差的优缺点)你的 template 尝试去 instantiate 一个 char f(A, char),但原本就已经有一个 void f(A, char) 了,如果你的编译器允许这个 instantiation 那就会造成有两个不同版本的 f(A, char),显然你的编译器不允许,所以才告诉你编译错误但我想文说的两个不同版本指的也可以是型别完全相同的两个不同 instantiation,但除非你的编译器有 bug 否则没办法借由改变程式码来造成这样的结果,所以才用这种相似的 example 给你看* 两个型别完全相同的 instance