Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 代码模板中的帮助_C++ - Fatal编程技术网

C++ 代码模板中的帮助

C++ 代码模板中的帮助,c++,C++,鉴于此代码 template <typename T> typename T::ElementT at (T const &a , T const &b) { return a[i] ; } 及 什么意思 由于T:ElementT是一个从属名称,因此您会在它前面看到关键字typename。它告诉编译器ElementT是一个测试类型,不是值 至于a[i],似乎T是一个定义了operator[]的类,当您编写a[i]时会调用它。例如,T可以是sample

鉴于此代码

template <typename T>
typename T::ElementT at (T const &a , T const &b)
{
        return a[i] ;
}

什么意思

由于
T:ElementT
是一个从属名称,因此您会在它前面看到关键字
typename
。它告诉编译器
ElementT
是一个测试类型,不是

至于
a[i]
,似乎
T
是一个定义了
operator[]
的类,当您编写
a[i]
时会调用它。例如,
T
可以是
sample
,定义如下(部分):

class sample
{
 public:
      typedef int ElementT; //nested type!

      //...

      ElementT operator[](int i) 
      {
          return m_data[i];
      }

      ElementT *m_data;
      //...
};

现在,如果
T
sample
,那么您可以编写
T::ElementT
以及
a[i]
类型
T
。在这种情况下,当
T
是sample时,我假设索引
I
的类型是
int

,我猜在这种情况下,代码T总是一个重载了运算符[]的类,并且有一个子类defention element,而没有这两个特性的任何其他类在编译时都会出错

<>这是Johannes在C++ FAQ中的一个令人费解的解释。p> 此操作通常称为“订阅”,并访问
a
中的第i个元素。为了做到这一点,
a
必须是数组或重载订阅运算符的某个类(如
std::vector
std::map
)。

然而,正如纳瓦兹在他的评论中指出的那样,由于
a
属于
T
类型,并且
T
预期具有嵌套类型
ElementAt
,因此在这种情况下
a
不能是数组

请问这段代码是从哪里来的?看起来很没用?它是否有这些错误(我认为应该是
consttypename T::ElementT
,以及
b
I
?)或者你把它们放进去了吗?在这种情况下,
a
不能是数组,因为
a
的类型是
T
,它有嵌套类型!
a[i]
typename T::ElementT 
class sample
{
 public:
      typedef int ElementT; //nested type!

      //...

      ElementT operator[](int i) 
      {
          return m_data[i];
      }

      ElementT *m_data;
      //...
};
typename T::ElementT 
a[i]