使用 std::is_same,为什么我的函数仍然不能用于 2 种类型
using std::is_same, why my function still can't work for 2 types
我正在尝试编写一个可以同时打印堆栈和队列的函数,我的代码如下
template<typename Cont>
void print_container(Cont& cont){
while(!cont.empty()){
if(std::is_same<Cont, stack<int>>::value){
auto elem = cont.top();
std::cout << elem << 'n';
} else {
auto elem = cont.front();
std::cout << elem << 'n';
}
cont.pop();
std::cout << elem << 'n';
}
}
int main(int argc, char *argv[])
{
stack<int> stk;
stk.push(1);
stk.push(2);
stk.push(3);
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
std::cout << "print stack" << endl;
print_container(stk);
std::cout << "print queue" << endl;
print_container(q);
return 0;
}
但它在这里不起作用,错误信息是:
demo_typeof.cpp:35:30: error: no member named 'front' in 'std::__1::stack<int, std::__1::deque<int, std::__1::allocator<int> > >'
auto elem = cont.front();
~~~~ ^
demo_typeof.cpp:52:5: note: in instantiation of function template specialization 'print_container<std::__1::stack<int, std::__1::deque<int, std::__1::allocator<int> > > >' requested here
print_container(stk);
^
demo_typeof.cpp:32:30: error: no member named 'top' in 'std::__1::queue<int, std::__1::deque<int, std::__1::allocator<int> > >'
auto elem = cont.top();
~~~~ ^
demo_typeof.cpp:54:5: note: in instantiation of function template specialization 'print_container<std::__1::queue<int, std::__1::deque<int, std::__1::allocator<int> > > >' requested here
print_container(q);
^
2 errors generated.
我知道这是有问题的,并且知道C++是静态类型的,没有太多的运行时支持。 但我想知道这不起作用的具体原因,以及如何处理它。
P.S.:判断容器类型的实际含义是:你可以简单地通过传递队列容器而不是堆栈将 DFS 函数更改为 BFS。因此,BFS和DFS可以共享大部分代码。
P.P.S:我处于 11 C++环境中,但也欢迎回答较旧或更高标准。
if
- 语句的两个分支都必须是可编译的else
,而这在您的情况下不是。基于部分专用化的众多可能解决方案之一,即使在 C++98 中也应该有效:
template <typename Cont>
struct element_accessor;
template <typename T>
struct element_accessor<std::stack<T>> {
const T& operator()(const std::stack<T>& s) const { return s.top(); }
};
template <typename T>
struct element_accessor<std::queue<T>> {
const T& operator()(const std::queue<T>& q) const { return q.front(); }
};
template<typename Cont>
void print_container(Cont& cont){
while(!cont.empty()){
auto elem = element_accessor<Cont>{}(cont);
std::cout << elem << 'n';
cont.pop();
}
}
具有if constexpr
的 C++17 解决方案:
template<template<class> typename Cont, typename T>
void print_container(Cont<T>& cont){
while(!cont.empty()){
if constexpr (std::is_same_v<Cont<T>, std::stack<T>>)
std::cout << cont.top() << 'n';
else if constexpr (std::is_same_v<Cont<T>, std::queue<T>>)
std::cout << cont.front() << 'n';
cont.pop();
}
}
我简单地使用重载来回答我自己的问题。
template<typename Elem>
Elem get_first_elem(stack<Elem>& cont){
return cont.top();
}
template<typename Elem>
Elem get_first_elem(queue<Elem>& cont){
return cont.front();
}
template<typename Cont>
void print_container(Cont& cont){
while(!cont.empty()){
auto elem = get_first_elem(cont);
cont.pop();
std::cout << elem << 'n';
}
}
问题
if(std::is_same<Cont, stack<int>>::value)
...
else
+++
是不是如果...
或+++
无法编译,那么您就无法使用它。 即使你只能采用一个分支或另一个分支,两个分支都会被编译,如果语法无效,那么你会收到编译器错误。 随着C++17的if constexpr
行为有所不同。 该条件将在编译时解决,并且只有采用的分支才会被实际编译。 其余代码将被丢弃。 切换到您的代码看起来像
template<typename Cont>
void print_container(Cont& cont){
while(!cont.empty()){
if constexpr(std::is_same<Cont, stack<int>>::value){
auto elem = cont.top();
std::cout << elem << 'n';
} else {
auto elem = cont.front();
std::cout << elem << 'n';
}
cont.pop();
std::cout << elem << 'n';
}
}
如果你可以使用 C++17,那么你想要的是if constexpr
#include <stack>
#include <queue>
#include <iostream>
using namespace std;
template<typename Cont>
void print_container(Cont& cont){
while(!cont.empty()){
if constexpr(std::is_same<Cont, stack<int>>::value){
auto elem = cont.top();
std::cout << elem << 'n';
} else {
auto elem = cont.front();
std::cout << elem << 'n';
}
cont.pop();
}
}
int main(int argc, char *argv[])
{
stack<int> stk;
stk.push(1);
stk.push(2);
stk.push(3);
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
std::cout << "print stack" << endl;
print_container(stk);
std::cout << "print queue" << endl;
print_container(q);
return 0;
}
相关文章:
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- "const wchar_t *" 类型的值不能用于初始化类型 "const PWSTR" 的实体
- 回调方法显示错误,类型为"void(*)"的值不能用于初始化类型的实体
- 使用 std::is_same,为什么我的函数仍然不能用于 2 种类型
- std::enable_if 不能用于禁用此声明
- 构造函数不能用于启发性化
- 类型值不能用于初始化类型实体
- 函数指针类型不能用于函数原型
- 为什么 constexpr 成员不能用于static_assert?
- 值类型 const char 不能用于初始化 char* 类型的实体
- 'this'不能用于常量表达式错误 (C++)
- "char *" 类型的值不能用于初始化类型 "char" 的实体
- 函数模板可用于本地lambda,但不能用于其他函数
- "const wchar_t *" 类型的值不能用于初始化类型为 "LPCSTR" 的实体
- char的值不能用于初始化char*的实体
- 为什么% 运算符不能用于 C/C++ 中的浮点数,但可以在 Java 和 C# 中使用
- 为什么 *从 const 成员函数返回的不能用于调用 C++ 中的其他函数
- 通过套接字接收的数据不能用于C++
- char*类型的值不能用于初始化“char”类型的实体
- 为什么auto不能用于重载函数