为什么用std::vector容器创建std::queue不会引发编译错误
Why std::queue creation with std::vector container does not raise compiler error
为什么std::queue
创建std::vector
容器不会引发编译器错误?
编译错误只在调用pop时发生(这很明显,因为vector不提供pop_front()
)。
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
queue<int, vector<int>> s;
s.push(10);
cout << s.front() << endl;
s.pop();
return 0;
}
演示因为类模板的成员函数在被调用之前不会隐式实例化。
From $14.7.1/2隐式实例化[temp.inst]:
除非类模板或成员模板的成员已经显式实例化或显式特化,即专门化的属性,在特化时隐式实例化成员在要求成员定义存在的上下文中引用;
和/4:
[ Example:
template<class T> struct Z {
void f();
void g();
};
void h() {
Z<int> a; // instantiation of class Z<int> required
Z<char>* p; // instantiation of class Z<char> not required
Z<double>* q; // instantiation of class Z<double> not required
a.f(); // instantiation of Z<int>::f() required
p->g(); // instantiation of class Z<char> required, and
// instantiation of Z<char>::g() required
}
和/11:
实现不能隐式实例化函数模板、变量模板、成员模板、非虚成员函数、成员类或类模板的静态数据成员不需要实例化。
相关文章:
- std::is_base_of表示ctor编译错误
- 关于std::move的使用,是否有编译警告
- std::experimental::source_location 在编译时
- C++如果必须在编译时确定大小,std::array 有什么意义?
- std::vector::p ush_back() 不会在 MSVC 上编译具有已删除移动构造函数的对象
- std::cout输出int时出现编译错误
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- std::unique_ptr 在 GCC 中工作,但不能在 Visual Studio 中编译
- 确保编译时的特定 std::array 位置
- 编译器不会使用 -std=c++11 编译智能指针
- GCC 8.3 无法编译 std::bind_front
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- std::less是否应该允许在编译时比较不相关的指针?
- 为什么 std::make_shared 无法编译带有已删除运算符 new 的类型?
- std::variant<>::get() 不能使用 Apple LLVM 10.0 编译
- 对带有唯一指针的 std::thread 使用类成员函数时出现编译错误
- QString和stdstring组合在std::编译错误中不起作用
- 使用typedef和std编译错误
- std::编译期间设置的数组类成员