Thus, templates are compiled twice:
Without instantiation, the template code itself is checked for correct syntax. Syntax errors are discovered, such as missing semicolons.
At the time of instantiation, the template code is checked to ensure that all calls are valid. Invalid calls are discovered, such as unsupported function calls.
This leads to an important problem in the handling of templates in practice: When a function template is used in a way that triggers its instantiation, a compiler will (at some point) need to see that template's definition. This breaks the usual compile and link distinction for ordinary functions, when the declaration of a function is sufficient to compile its use. Methods of handling this problem are discussed in Chapter 6. For the moment, let's take the sim... (查看原文)
So far, we have looked at cases in which either all or none of the function template arguments were mentioned explicitly. Another approach is to specify only the first arguments explicitly and to allow the deduction process to derive the rest. In general, you must specify all the argument types up to the last argument type that cannot be determined implicitly. Thus, if you change the order of t...
2012-02-16 01:49
So far, we have looked at cases in which either all or none of the function template arguments were mentioned explicitly. Another approach is to specify only the first arguments explicitly and to allow the deduction process to derive the rest. In general, you must specify all the argument types up to the last argument type that cannot be determined implicitly. Thus, if you change the order of the template parameters in our example, the caller needs to specify only the return type:
template <typename RT, typename T1, typename T2>
inline RT max (T1 const& a, T2 const& b);
…
max<double>(4,4.2) // OK: return type is double
In this example, the call to max<double> explicitly sets RT to double, but the parameters T1 and T2 are deduced to be int and double from the arguments.
Cast the arguments so that they both match: max(static_cast<double>(4),4.2) // OK Specify (or qualify) explicitly the type of T: max<double>(4,4.2) // OK
Thus, templates are compiled twice: Without instantiation, the template code itself is checked for correct syntax. Syntax errors are discovered, such as missing semicolons. At the time of instantiation, the template code is checked to ensure that all calls are valid. Invalid calls are discovered, such as unsupported function calls. This leads to an important problem in the handling of templates...
2012-02-16 01:04
Thus, templates are compiled twice:
Without instantiation, the template code itself is checked for correct syntax. Syntax errors are discovered, such as missing semicolons.
At the time of instantiation, the template code is checked to ensure that all calls are valid. Invalid calls are discovered, such as unsupported function calls.
This leads to an important problem in the handling of templates in practice: When a function template is used in a way that triggers its instantiation, a compiler will (at some point) need to see that template's definition. This breaks the usual compile and link distinction for ordinary functions, when the declaration of a function is sufficient to compile its use. Methods of handling this problem are discussed in Chapter 6. For the moment, let's take the simplest approach: Each template is implemented inside a header file by using inline functions.引自 2.1.2 Using the Template
So far, we have looked at cases in which either all or none of the function template arguments were mentioned explicitly. Another approach is to specify only the first arguments explicitly and to allow the deduction process to derive the rest. In general, you must specify all the argument types up to the last argument type that cannot be determined implicitly. Thus, if you change the order of t...
2012-02-16 01:49
So far, we have looked at cases in which either all or none of the function template arguments were mentioned explicitly. Another approach is to specify only the first arguments explicitly and to allow the deduction process to derive the rest. In general, you must specify all the argument types up to the last argument type that cannot be determined implicitly. Thus, if you change the order of the template parameters in our example, the caller needs to specify only the return type:
template <typename RT, typename T1, typename T2>
inline RT max (T1 const& a, T2 const& b);
…
max<double>(4,4.2) // OK: return type is double
In this example, the call to max<double> explicitly sets RT to double, but the parameters T1 and T2 are deduced to be int and double from the arguments.
Cast the arguments so that they both match: max(static_cast<double>(4),4.2) // OK Specify (or qualify) explicitly the type of T: max<double>(4,4.2) // OK
Thus, templates are compiled twice: Without instantiation, the template code itself is checked for correct syntax. Syntax errors are discovered, such as missing semicolons. At the time of instantiation, the template code is checked to ensure that all calls are valid. Invalid calls are discovered, such as unsupported function calls. This leads to an important problem in the handling of templates...
2012-02-16 01:04
Thus, templates are compiled twice:
Without instantiation, the template code itself is checked for correct syntax. Syntax errors are discovered, such as missing semicolons.
At the time of instantiation, the template code is checked to ensure that all calls are valid. Invalid calls are discovered, such as unsupported function calls.
This leads to an important problem in the handling of templates in practice: When a function template is used in a way that triggers its instantiation, a compiler will (at some point) need to see that template's definition. This breaks the usual compile and link distinction for ordinary functions, when the declaration of a function is sufficient to compile its use. Methods of handling this problem are discussed in Chapter 6. For the moment, let's take the simplest approach: Each template is implemented inside a header file by using inline functions.引自 2.1.2 Using the Template
So far, we have looked at cases in which either all or none of the function template arguments were mentioned explicitly. Another approach is to specify only the first arguments explicitly and to allow the deduction process to derive the rest. In general, you must specify all the argument types up to the last argument type that cannot be determined implicitly. Thus, if you change the order of t...
2012-02-16 01:49
So far, we have looked at cases in which either all or none of the function template arguments were mentioned explicitly. Another approach is to specify only the first arguments explicitly and to allow the deduction process to derive the rest. In general, you must specify all the argument types up to the last argument type that cannot be determined implicitly. Thus, if you change the order of the template parameters in our example, the caller needs to specify only the return type:
template <typename RT, typename T1, typename T2>
inline RT max (T1 const& a, T2 const& b);
…
max<double>(4,4.2) // OK: return type is double
In this example, the call to max<double> explicitly sets RT to double, but the parameters T1 and T2 are deduced to be int and double from the arguments.
Cast the arguments so that they both match: max(static_cast<double>(4),4.2) // OK Specify (or qualify) explicitly the type of T: max<double>(4,4.2) // OK
Thus, templates are compiled twice: Without instantiation, the template code itself is checked for correct syntax. Syntax errors are discovered, such as missing semicolons. At the time of instantiation, the template code is checked to ensure that all calls are valid. Invalid calls are discovered, such as unsupported function calls. This leads to an important problem in the handling of templates...
2012-02-16 01:04
Thus, templates are compiled twice:
Without instantiation, the template code itself is checked for correct syntax. Syntax errors are discovered, such as missing semicolons.
At the time of instantiation, the template code is checked to ensure that all calls are valid. Invalid calls are discovered, such as unsupported function calls.
This leads to an important problem in the handling of templates in practice: When a function template is used in a way that triggers its instantiation, a compiler will (at some point) need to see that template's definition. This breaks the usual compile and link distinction for ordinary functions, when the declaration of a function is sufficient to compile its use. Methods of handling this problem are discussed in Chapter 6. For the moment, let's take the simplest approach: Each template is implemented inside a header file by using inline functions.引自 2.1.2 Using the Template
0 有用 小K 2012-05-19
强大的模板,好吧,我只想读懂stl、boost的源码而已
0 有用 丸子 2014-08-15
C++已经不是人类能掌握的了。
0 有用 剑南 2012-03-15
没跟着写代码,基本没看懂。
0 有用 戈多 2009-01-17
C++反人类的证明之一。
0 有用 lilde90 2015-10-16
建议看第一部分和第三部分的部分,第二部分谨慎吧,另外还是建议,不要浪费生命在C++ templates的高级用法上了,更加不要在多人维护的项目中使用这些高级用法了。害人害己
0 有用 赵映 2019-03-31
看的中文版,很丰富。但涉及到元编程的不多
0 有用 Gearslogy 2017-12-31
看过openvdb API,如果你看不懂,必须看这本书。读到表达式模版已经够用了
0 有用 AperSpike 2017-12-20
这本书的part 2是要硬啃下来的,细节太琐碎。
0 有用 lyx2007825 2017-09-18
模板可以读这本书入门,囊括的东西还是蛮多的
0 有用 lilde90 2015-10-16
建议看第一部分和第三部分的部分,第二部分谨慎吧,另外还是建议,不要浪费生命在C++ templates的高级用法上了,更加不要在多人维护的项目中使用这些高级用法了。害人害己