簡體   English   中英

為什么使用std :: vector容器創建std :: queue不會引發編譯器錯誤

[英]Why std::queue creation with std::vector container does not raise compiler error

為什么使用std::vector容器創建std::queue不會引發編譯錯誤?

只有在調用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;
}

DEMO

因為類模板的成員函數在被調用之前不會被隱式實例化。

來自$ 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:

實現不應隱式實例化函數模板,變量模板,成員模板,非虛擬成員函數,成員類或不需要實例化的類模板的靜態數據成員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM