为什么我不能将数字模板参数传递给我的模板函数?
Why can I not pass a numeric template parameter to my templated function?
我有一个自定义 class 封装了一个 std::tuple
(“MyTuple”),另一个 class 实现了一个 std::tuple
(“MyInterface”)的自定义接口。我在代码库中需要这个单独的接口,下面的代码被简化了。
由于 std::tuple
的元素需要使用键作为模板参数来访问,接口的函数有一个数字模板参数 size_t Key
然后将其提供给 std::get
作为元组例如。
这个接口工作正常,但是当从另一个将数字参数作为“key”传递的模板函数调用它时,就不行了:
#include <iostream>
#include <functional>
#include <tuple>
#include <string>
template <typename... Types>
class MyInterface {
public:
MyInterface(const std::tuple<Types...>& tuple) : tuple(tuple) {}
template <size_t Key>
std::string getString() {
return std::to_string(std::get<Key>(tuple));
}
private:
const std::tuple<Types...>& tuple;
};
template <typename... Types>
class MyTuple {
public:
MyTuple(Types... values) : value(std::tuple<Types...>(values...)) {}
template <size_t Key>
std::string asString() {
MyInterface<Types...> interface(value);
return interface.getString<Key>(); // here I get the compiler error
}
private:
std::tuple<Types...> value;
};
int main() {
MyInterface<int, float, long> interface(std::tuple<int, float, long>(7, 3.3, 40));
std::cout << interface.getString<0>() << std::endl; // this works fine
MyTuple<int, float, long> tuple(7, 3.3, 40);
std::cout << tuple.asString<0>() << std::endl;
}
g++的完整输出:
templated_function_parameter_pack.cpp: In member function ‘std::__cxx11::string MyTuple<Types>::asString()’:
templated_function_parameter_pack.cpp:28:39: error: expected primary-expression before ‘)’ token
return interface.getString<Key>(); // here I get the compiler error
^
templated_function_parameter_pack.cpp: In instantiation of ‘std::__cxx11::string MyTuple<Types>::asString() [with long unsigned int Key = 0; Types = {int, float, long int}; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
templated_function_parameter_pack.cpp:40:34: required from here
templated_function_parameter_pack.cpp:28:33: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘long unsigned int’ to binary ‘operator<’
return interface.getString<Key>(); // here I get the compiler error
为什么在 MyTuple::asString<size_t Key>
中调用 interface.getString<Key>()
的语法无效?
当要调用实例的模板方法时,需要这样写:
return interface.template getString<Key>();
您会在这个答案中找到所有原因的详细信息:Where and why do I have to put the "template" and "typename" keywords?
我有一个自定义 class 封装了一个 std::tuple
(“MyTuple”),另一个 class 实现了一个 std::tuple
(“MyInterface”)的自定义接口。我在代码库中需要这个单独的接口,下面的代码被简化了。
由于 std::tuple
的元素需要使用键作为模板参数来访问,接口的函数有一个数字模板参数 size_t Key
然后将其提供给 std::get
作为元组例如。
这个接口工作正常,但是当从另一个将数字参数作为“key”传递的模板函数调用它时,就不行了:
#include <iostream>
#include <functional>
#include <tuple>
#include <string>
template <typename... Types>
class MyInterface {
public:
MyInterface(const std::tuple<Types...>& tuple) : tuple(tuple) {}
template <size_t Key>
std::string getString() {
return std::to_string(std::get<Key>(tuple));
}
private:
const std::tuple<Types...>& tuple;
};
template <typename... Types>
class MyTuple {
public:
MyTuple(Types... values) : value(std::tuple<Types...>(values...)) {}
template <size_t Key>
std::string asString() {
MyInterface<Types...> interface(value);
return interface.getString<Key>(); // here I get the compiler error
}
private:
std::tuple<Types...> value;
};
int main() {
MyInterface<int, float, long> interface(std::tuple<int, float, long>(7, 3.3, 40));
std::cout << interface.getString<0>() << std::endl; // this works fine
MyTuple<int, float, long> tuple(7, 3.3, 40);
std::cout << tuple.asString<0>() << std::endl;
}
g++的完整输出:
templated_function_parameter_pack.cpp: In member function ‘std::__cxx11::string MyTuple<Types>::asString()’:
templated_function_parameter_pack.cpp:28:39: error: expected primary-expression before ‘)’ token
return interface.getString<Key>(); // here I get the compiler error
^
templated_function_parameter_pack.cpp: In instantiation of ‘std::__cxx11::string MyTuple<Types>::asString() [with long unsigned int Key = 0; Types = {int, float, long int}; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
templated_function_parameter_pack.cpp:40:34: required from here
templated_function_parameter_pack.cpp:28:33: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘long unsigned int’ to binary ‘operator<’
return interface.getString<Key>(); // here I get the compiler error
为什么在 MyTuple::asString<size_t Key>
中调用 interface.getString<Key>()
的语法无效?
当要调用实例的模板方法时,需要这样写:
return interface.template getString<Key>();
您会在这个答案中找到所有原因的详细信息:Where and why do I have to put the "template" and "typename" keywords?