q09*_*987 4 c++ templates boost name-lookup
#include <iostream>
#include <boost/static_assert.hpp>
using namespace std;
// I understand how the following template function works
// template <class T>
// T GetMax (T a, T b) {
// T result;
// result = (a>b)? a : b;
// return (result);
// }
// I have difficulties to understand how the following code works
// when we should use this syntax
template<int i> void accepts_values_between_1_and_10() {
BOOST_STATIC_ASSERT(i >=1 && i < 10);
cout << "i(between 1 and 10): " << i << endl;
}
// I created the following function to test the understanding of BOOST_STATIC_ASSERT
template<typename T> void accepts_values_between_1_and_10_alternative(T i) {
BOOST_STATIC_ASSERT(i >=1 && i < 10);
cout << "i(between 1 and 10): " << i << endl;
}
int main () {
const int i = 5;
accepts_values_between_1_and_10<i>();
const int j = 6;
accepts_values_between_1_and_10_alternative(j);
return 0;
}
// $> g++ -o template_function -Wall -g template_function.cpp
// template_function.cpp: In function ‘void accepts_values_between_1_and_10_alternative(T) [with T = int]’:
// template_function.cpp:33:48: instantiated from here
// template_function.cpp:23:1: error: ‘((i > 0) && (i <= 9))’ is not a valid template argument for type ‘bool’ because it is a non-constant expression
Run Code Online (Sandbox Code Playgroud)
问题1 >以下语句背后的语法是什么?什么时候应该使用它?如果它是模板专业化,为什么我们必须提供参数而不仅仅是
template<int> void accepts_values_between_1_and_10()
instead of
template<int i> void accepts_values_between_1_and_10()
template<int> void accepts_values_between_1_and_10(int i)
instead of
template<int i> void accepts_values_between_1_and_10()
Run Code Online (Sandbox Code Playgroud)
问题2 >如果我们必须使用此表单,那么我们必须在函数范围内采用这种语法吗?
问题3 >如何纠正定义accepts_values_between_1_and_10_alternative以使其适用BOOST_STATIC_ASSERT?
谢谢
您template<typename T> void accepts_values_between_1_and_10_alternative(T i)没有编译,因为在实例化模板后评估运行时参数.在函数范围,语句BOOST_STATIC_ASSERT(i >=1 && i < 10);意味着i被查找为非类型模板参数.然而,在封闭命名空间范围,i是针对一个运行时参数仅其类型是抬头实例int为T.但是值6(即使它是程序中的常量表达式)仅在模板实例化之后进行计算,并且在模板参数推导期间在函数范围内不可见.
为了您template<int i> void accepts_values_between_1_and_10()的值的i正抬头实例化5的i,并且这个值被传播回BOOST_STATIC_ASSERT.