C++模板默认参数
C++ template default parameter
我知道简单的模板和模板专业化是如何工作的,但我对此感到困惑。
程序第一行的T t = T()
是干什么的?这是默认参数吗?以及如何确定程序的输出?
#include <iostream>
template<class T, T t = T()>
class A
{
private:
template<bool b>
class B
{
public:
static const int m_n = b ? 1 : 0;
};
public:
static const int m_value = B<(t > T())>::m_n - B<(t < T())>::m_n;
};
int main()
{
std::cout << A<int, -9>::m_value
<< A<bool, true>::m_value
<< A<char>::m_value << std::endl;
return 0;
}
这是一道关于 C++ 评估测试的问题,我正在努力理解。
是的。第二个参数是这个模板的默认参数。
如果你知道这一点,输出的确定应该是相当直接的。我会为你做第一个:
A<int, -9>::m_value
int
是T
使用的数据类型,int t
的值为-9
.
这一行:
static const int m_value = B<(t > T())>::m_n - B<(t < T())>::m_n;
这样计算(int()
为零):
static const int m_value = B<(-9 > 0)>::m_n - B<(-9 < 0)>::m_n;
评估如下:
static const int m_value = B<false>::m_n - B<true>::m_n;
评估如下:
static const int m_value = 0 - 1;
最终评价为:
static const int m_value = -1;
所以:
std::cout << A<int, -9>::m_value
等同于:
std::cout << -1
现在尝试自己解决剩下的问题。
是的,这是 class 模板的默认参数示例。您会发现这个示例非常有用
https://msdn.microsoft.com/en-us/library/bys786s7.aspx
简而言之,是的,它确实为第二个模板参数提供了默认值。您会在行 A<char>::m_value
中看到 T t = T()
的使用。由于第二个模板参数被初始化为 T()
(T
的默认构造函数),因此 t
默认采用您提供的任何类型的默认值作为第一个模板参数。然后程序将作为第二个模板参数给出的值与作为第一个模板参数给出的类型的默认值进行比较。把它想象成下面的函数,如果我理解 class 正确,做同样的事情。
template<class T>
// T t = T() in a function is the same as your T t = T()
// in your template parameters
int f(T t = T())
{
return (T() == t) ? 0 : ((T() < t) ? 1 : -1);
}
用法:
int main(int argc, char *argv[]) {
std::cout << f<int(-9)
<< A<bool>(true)
<< A<char>() << std::endl;
}
函数returns 0 如果t
等于类型T
的默认值,-1 如果t
小于类型[=15] 的默认值=],如果 t
大于类型 T
的默认值,则 +1。
我知道简单的模板和模板专业化是如何工作的,但我对此感到困惑。
程序第一行的T t = T()
是干什么的?这是默认参数吗?以及如何确定程序的输出?
#include <iostream>
template<class T, T t = T()>
class A
{
private:
template<bool b>
class B
{
public:
static const int m_n = b ? 1 : 0;
};
public:
static const int m_value = B<(t > T())>::m_n - B<(t < T())>::m_n;
};
int main()
{
std::cout << A<int, -9>::m_value
<< A<bool, true>::m_value
<< A<char>::m_value << std::endl;
return 0;
}
这是一道关于 C++ 评估测试的问题,我正在努力理解。
是的。第二个参数是这个模板的默认参数。
如果你知道这一点,输出的确定应该是相当直接的。我会为你做第一个:
A<int, -9>::m_value
int
是T
使用的数据类型,int t
的值为-9
.
这一行:
static const int m_value = B<(t > T())>::m_n - B<(t < T())>::m_n;
这样计算(int()
为零):
static const int m_value = B<(-9 > 0)>::m_n - B<(-9 < 0)>::m_n;
评估如下:
static const int m_value = B<false>::m_n - B<true>::m_n;
评估如下:
static const int m_value = 0 - 1;
最终评价为:
static const int m_value = -1;
所以:
std::cout << A<int, -9>::m_value
等同于:
std::cout << -1
现在尝试自己解决剩下的问题。
是的,这是 class 模板的默认参数示例。您会发现这个示例非常有用 https://msdn.microsoft.com/en-us/library/bys786s7.aspx
简而言之,是的,它确实为第二个模板参数提供了默认值。您会在行 A<char>::m_value
中看到 T t = T()
的使用。由于第二个模板参数被初始化为 T()
(T
的默认构造函数),因此 t
默认采用您提供的任何类型的默认值作为第一个模板参数。然后程序将作为第二个模板参数给出的值与作为第一个模板参数给出的类型的默认值进行比较。把它想象成下面的函数,如果我理解 class 正确,做同样的事情。
template<class T>
// T t = T() in a function is the same as your T t = T()
// in your template parameters
int f(T t = T())
{
return (T() == t) ? 0 : ((T() < t) ? 1 : -1);
}
用法:
int main(int argc, char *argv[]) {
std::cout << f<int(-9)
<< A<bool>(true)
<< A<char>() << std::endl;
}
函数returns 0 如果t
等于类型T
的默认值,-1 如果t
小于类型[=15] 的默认值=],如果 t
大于类型 T
的默认值,则 +1。