在什么情况下,用于实例化模板的参数不可用? 本文首次将性状引入C++,并着重介绍以下几个方面: template <class numT> class matrix { public: typedef numT num_type; typedef float_traits<num_type> traits_type; inline num_type epsilon() { return traits_type::epsilon(); } ... }; 模板 类矩阵{ 公众: typedef num_type; typedef float_traits_type; 内联num_type epsilon(){return traits_type::epsilon();} ... };

在什么情况下,用于实例化模板的参数不可用? 本文首次将性状引入C++,并着重介绍以下几个方面: template <class numT> class matrix { public: typedef numT num_type; typedef float_traits<num_type> traits_type; inline num_type epsilon() { return traits_type::epsilon(); } ... }; 模板 类矩阵{ 公众: typedef num_type; typedef float_traits_type; 内联num_type epsilon(){return traits_type::epsilon();} ... };,c++,templates,traits,C++,Templates,Traits,请注意,在迄今为止的所有示例中,每个模板都提供了其参数的publictypedefs,以及依赖于它们的任何内容这并非偶然:在各种情况下,用于实例化模板的参数都不可用,只有在模板声明中作为typedefs提供时才能检索到这些参数。寓意:始终提供这些typedef 但让我困惑的是,没有显示需要使用typedef的情况。有人能解释一下吗 假设您有一个函数模板,它接受T的通用容器: template <typename ContainerT> void DoThings(ContainerT

请注意,在迄今为止的所有示例中,每个模板都提供了其参数的public
typedef
s,以及依赖于它们的任何内容这并非偶然:在各种情况下,用于实例化模板的参数都不可用,只有在模板声明中作为
typedef
s提供时才能检索到这些参数。
寓意:始终提供这些
typedef


但让我困惑的是,没有显示需要使用
typedef
的情况。有人能解释一下吗

假设您有一个函数模板,它接受
T
的通用容器:

template <typename ContainerT>
void DoThings(ContainerT const& input)
{
   // Do something
}
我们不知道什么是
t
,容器中包含的类型是;在这个函数模板中,我们只有容器本身的类型。为了得到容器中的类型,我们需要容器来帮助我们,告诉我们它包含的类型是什么。容器类通过
typedef
s实现这一点。对于标准容器,迭代器类型为
iterator
,包含的值类型为
value\u type

template <typename ContainerT>
void DoThings(ContainerT const& input)
{
   // Container provides its iterator type via a typedef.
   typename ContainerT::iterator it = input.begin();

   // Container provides its contained type via a typedef.
   typename ContainerT::value_type firstElement = *it;
}
有时,您仍然希望能够获取用于其他目的的实际类型:

// Failure to compile: http://ideone.com/vxJ2IU
// Successful compile: http://ideone.com/b5fU3S
#include <vector>
#include <list>
#include <deque>
#include <type_traits>
#include <iostream>

template <typename ContainerT>
void DoThings(ContainerT const& input)
{
   // DoThings only accepts integral containers:
   static_assert(
      std::is_integral<typename ContainerT::value_type>::value,
      "DoThings requires that the contained type be integral");
   // Make the compiler figure it out:
   auto it = input.begin();
   auto firstElement = *it;
   std::cout << firstElement;
}

int main()
{
    std::vector<int> abc;
    abc.push_back(42);

    std::list<long> def;
    def.push_back(1729);

    std::deque<short> queue;
    queue.push_back(1234);

    DoThings(abc);
    DoThings(def);
    DoThings(queue);

    // Does not compile due to static assert:
    std::vector<double> doubles;
    doubles.push_back(3.14);
    DoThings(doubles);
}
//编译失败:http://ideone.com/vxJ2IU
//成功编译:http://ideone.com/b5fU3S
#包括
#包括
#包括
#包括
#包括
模板
void DoThings(ContainerT常量和输入)
{
//DoThings仅接受整数容器:
静态断言(
std::is_integral::value,
“DoThings要求包含的类型为整数”);
//让编译器明白这一点:
auto it=input.begin();
自动第一元素=*它;

std::cout假设您有一个函数模板,它接受一个通用容器
T

template <typename ContainerT>
void DoThings(ContainerT const& input)
{
   // Do something
}
我们不知道什么是
t
,容器中包含的类型是什么;在这个函数模板中我们只有容器本身的类型。为了获得容器中的类型,我们需要容器帮我们一点忙,告诉我们它包含的类型是什么。容器类通过
typedef
s来实现这一点。对于t对于标准容器,迭代器类型为
iterator
,对于包含的值类型为
value\u type

template <typename ContainerT>
void DoThings(ContainerT const& input)
{
   // Container provides its iterator type via a typedef.
   typename ContainerT::iterator it = input.begin();

   // Container provides its contained type via a typedef.
   typename ContainerT::value_type firstElement = *it;
}
有时,您仍然希望能够获取用于其他目的的实际类型:

// Failure to compile: http://ideone.com/vxJ2IU
// Successful compile: http://ideone.com/b5fU3S
#include <vector>
#include <list>
#include <deque>
#include <type_traits>
#include <iostream>

template <typename ContainerT>
void DoThings(ContainerT const& input)
{
   // DoThings only accepts integral containers:
   static_assert(
      std::is_integral<typename ContainerT::value_type>::value,
      "DoThings requires that the contained type be integral");
   // Make the compiler figure it out:
   auto it = input.begin();
   auto firstElement = *it;
   std::cout << firstElement;
}

int main()
{
    std::vector<int> abc;
    abc.push_back(42);

    std::list<long> def;
    def.push_back(1729);

    std::deque<short> queue;
    queue.push_back(1234);

    DoThings(abc);
    DoThings(def);
    DoThings(queue);

    // Does not compile due to static assert:
    std::vector<double> doubles;
    doubles.push_back(3.14);
    DoThings(doubles);
}
//编译失败:http://ideone.com/vxJ2IU
//成功编译:http://ideone.com/b5fU3S
#包括
#包括
#包括
#包括
#包括
模板
void DoThings(ContainerT常量和输入)
{
//DoThings仅接受整数容器:
静态断言(
std::is_integral::value,
“DoThings要求包含的类型为整数”);
//让编译器明白这一点:
auto it=input.begin();
自动第一元素=*它;
std::cout一个简单的例子:


假设您想编写一个通用的sum函数,对
STL容器中的所有元素进行求和。它可以是
vector
list
set
。其内容可能是
int
float
string
(对于字符串,sum将是串联的)(保持简单)

如果容器包含
int
,那么总和将是
int
。如果容器包含
浮动
,那么总和将不是
int
,而是一个
浮动
点。对于
字符串
,它应该是
字符串
。其他所有内容都是相同的(函数内的操作)


写这篇文章的一种方法是

template<typename T>
T sum(const vector<T>& t) {
   T total = T();
   // iterate and sum.
   return total;
}
但是这里返回什么呢?。您如何知道容器包含什么。这里是
的作用所在。幸运的STL容器有两个typedef,可以让您了解它们所做的事情和能够做的事情。。在我们的例子中,它们将包含的类型定义为
值ype
(我确信您在某种程度上使用了
C::iterator
,它也是一个typedef)


现在我们的小和函数可以写成

template<typename T>
typename
T::value_type sum(const T& t) {
   typedef typename T::value_type v_type;;
   v_type total = v_type();
   // iterate and sum.
   return total;
}
这对其他使用
C
的人很有用。他们可以很容易地找到
T
V
类型的对象
C
被键入的类型。

一个简单的例子:


假设您想编写一个通用的sum函数,对
STL容器中的所有元素进行求和。它可以是
vector
list
set
。其内容可能是
int
float
string
(对于字符串,sum将是串联的)(保持简单)

如果容器包含
int
,那么总和将是
int
。如果容器包含
浮动
,那么总和将不是
int
,而是一个
浮动
点。对于
字符串
,它应该是
字符串
。其他所有内容都是相同的(函数内的操作)


写这篇文章的一种方法是

template<typename T>
T sum(const vector<T>& t) {
   T total = T();
   // iterate and sum.
   return total;
}
但是这里返回什么呢?。您如何知道容器包含什么。这里是
的作用所在。幸运的STL容器有两个typedef,可以让您了解它们所做的事情和能够做的事情。。在我们的例子中,它们将包含的类型定义为
值ype
(我确信您在某些时候使用过
C::iterator
template<typename T, typename V>
class C {
    typedef T t_type;
    typedef V v_type;
    //
    //
}