如何检查可变参数模板中的非原始类型?
How to check for non primitive types in variadic templates?
我正在尝试编写一个名为 print()
的实用函数,其中如果传递了原始类型,它只是打印它,如果传递了原始类型的 std::vector<int>
,它必须循环通过它并打印出来。我尝试了 int
的 std::vector
但惨遭失败,主要是因为我不确定这在 C++ 中是否允许。如果您可以提供一些通用的方法来为所有原始类型的向量执行此操作,那将会很有帮助。
#include <iostream>
#include <vector>
#include <type_traits>
void print(std::ostream& out) {}
template <typename T, typename... Args>
void print(std::ostream& out, T type, Args... args)
{
if (std::is_same<T, std::vector<int>>::value) {
for (auto vector_iterator = type.begin();
vector_iterator != type.end(); ++vector_iterator) {
out << (*vector_iterator) << " ";
}
} else {
out << type;
}
print(out, args...);
}
int main()
{
std::vector<int> v {4,5,6};
print(std::cout, "bla", 1, 23.0, v);
return 0;
}
这是我遇到的错误,如果有帮助的话:
util.cc: In instantiation of 'void print(std::ostream&, T, Args ...) [with T = const char*; Args = {int, double, std::vector<int, std::allocator<int> >}; std::ostream = std::basic_ostream<char>]':
util.cc:26:36: required from here
util.cc:11:42: error: request for member 'begin' in 'type', which is of non-class type 'const char*'
for (auto vector_iterator = type.begin();
^
util.cc:12:20: error: request for member 'end' in 'type', which is of non-class type 'const char*'
vector_iterator != type.end(); ++vector_iterator) {
谢谢
如果能编译C++17,就可以用if constexpr
if constexpr (std::is_same<T, std::vector<int>>::value)
在 C++17 之前,您必须开发不同的函数,因为 type.begin()
/type.end()
部分也会在 value
为 false 时编译(同样当 T
为 false 时不是 std::vector
).
"split" 函数的可能方法如下
template <typename... Args>
void print (std::ostream& out, std::vector<int> const & v, Args... args)
{
for (auto const & i : v)
out << i << ' ';
print(out, args...);
}
template <typename T, typename... Args>
void print (std::ostream& out, T type, Args... args)
{
out << type;
print(out, args...);
}
或者,如果你想拦截不同的std::vector
类型,你可以将std::vector
的类型T
模板化,第一个函数变成
template <typename T, typename... Args>
void print (std::ostream& out, std::vector<T> const & v, Args... args)
{
for (auto const & i : v)
out << i << ' ';
print(out, args...);
}
我正在尝试编写一个名为 print()
的实用函数,其中如果传递了原始类型,它只是打印它,如果传递了原始类型的 std::vector<int>
,它必须循环通过它并打印出来。我尝试了 int
的 std::vector
但惨遭失败,主要是因为我不确定这在 C++ 中是否允许。如果您可以提供一些通用的方法来为所有原始类型的向量执行此操作,那将会很有帮助。
#include <iostream>
#include <vector>
#include <type_traits>
void print(std::ostream& out) {}
template <typename T, typename... Args>
void print(std::ostream& out, T type, Args... args)
{
if (std::is_same<T, std::vector<int>>::value) {
for (auto vector_iterator = type.begin();
vector_iterator != type.end(); ++vector_iterator) {
out << (*vector_iterator) << " ";
}
} else {
out << type;
}
print(out, args...);
}
int main()
{
std::vector<int> v {4,5,6};
print(std::cout, "bla", 1, 23.0, v);
return 0;
}
这是我遇到的错误,如果有帮助的话:
util.cc: In instantiation of 'void print(std::ostream&, T, Args ...) [with T = const char*; Args = {int, double, std::vector<int, std::allocator<int> >}; std::ostream = std::basic_ostream<char>]':
util.cc:26:36: required from here
util.cc:11:42: error: request for member 'begin' in 'type', which is of non-class type 'const char*'
for (auto vector_iterator = type.begin();
^
util.cc:12:20: error: request for member 'end' in 'type', which is of non-class type 'const char*'
vector_iterator != type.end(); ++vector_iterator) {
谢谢
如果能编译C++17,就可以用if constexpr
if constexpr (std::is_same<T, std::vector<int>>::value)
在 C++17 之前,您必须开发不同的函数,因为 type.begin()
/type.end()
部分也会在 value
为 false 时编译(同样当 T
为 false 时不是 std::vector
).
"split" 函数的可能方法如下
template <typename... Args>
void print (std::ostream& out, std::vector<int> const & v, Args... args)
{
for (auto const & i : v)
out << i << ' ';
print(out, args...);
}
template <typename T, typename... Args>
void print (std::ostream& out, T type, Args... args)
{
out << type;
print(out, args...);
}
或者,如果你想拦截不同的std::vector
类型,你可以将std::vector
的类型T
模板化,第一个函数变成
template <typename T, typename... Args>
void print (std::ostream& out, std::vector<T> const & v, Args... args)
{
for (auto const & i : v)
out << i << ' ';
print(out, args...);
}