"期望的表达"在尝试定义嵌套模板成员函数时

时间:2016-09-16 20:01:49

标签: c++ templates

我有一个模板化的类,它有一个模板化的成员函数。

目前,我将定义命名为:

template <typename... Ts>
template <size_t Column>
At<Column, Ts...> & tuple_reference<Ts...>::at(void) {
  return array.at<Column>()[row];
}

但是我收到了这个错误:

$ g++ example.cpp -o example -Wall -Werror -Wextra -std=c++1y -stdlib=libc++
example.cpp:42:27: error: expected expression
  return array.at<Column>()[row];
                          ^
1 error generated.

这使我感到困惑,因为array的方法在我的代码中的其他位置正常工作(请参阅下面的ic_arr.at<1>())。

这是我的全部资料来源:

#include <iostream>

// Provide a Array-of-Structs (AoS) interface to a collection that
// is actually implemented as a Struct-of-Arrays (SoA).
namespace soa { 
template <typename... Ts> class tuple_array;

// index into a parameter pack to get the N'th type listed
template <int N, typename... Ts>
using At = typename std::tuple_element<N, std::tuple<Ts...>>::type;

// the "Struct" the API provides
template <typename... Ts> class tuple_reference { 
private:
  tuple_array<Ts...> &array;
  size_t row;
public:
  tuple_reference(tuple_array<Ts...> &array, size_t const &row)
      : array{array}, row{row} {}
  // extract the value for a given index
  template <size_t Column>
  At<Column, Ts...> & at(void);
};

// The toplevel interface
template <typename... Ts> class tuple_array { 
private:
  std::tuple<Ts *...> arrays;
  size_t size{};
public:
  tuple_array(Ts *... arrays, size_t const &size)
      : arrays{arrays...}, size{size} {}
  // extract the array for a given index
  template <size_t Column> At<Column, Ts *...> &at(void) { 
    return std::get<Column>(arrays);
  } 
};

template <typename... Ts>
template <size_t Column>
At<Column, Ts...> & tuple_reference<Ts...>::at(void) { 
  return array.at<Column>()[row];
} 
} 
int main() { 
  int five_ints[]{0, 1, 2, 3, 4};
  char six_chars[]{"abcde"};

  soa::tuple_array<int, char> ic_arr{five_ints, six_chars, 5};
  soa::tuple_reference<int, char> ic_ref{ic_arr, 0};

  // outputs "abcde\n"
  std::cout << ic_arr.at<1>() << std::endl;
  // should output "0 a\n"
  std::cout << ic_ref.at<0>() << " " << ic_ref.at<1>() << std::endl;
}

我正在使用Apple LLVM version 6.0 (clang-600.0.57)

1 个答案:

答案 0 :(得分:1)

arraytuple_reference中的dependent name

应该是:

return array.template at<Ix>()[ix];

有关详细信息,请参阅here