在模板,在那里,为什么我必须把typename和template上依赖的名字呢?究竟什么是依赖名称?我有以下代码:
template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
// ...
template<typename U> struct inUnion {
// Q: where to add typename/template here?
typedef Tail::inUnion<U> dummy;
};
template< > struct inUnion<T> {
};
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
// ...
template<typename U> struct inUnion {
char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …Run Code Online (Sandbox Code Playgroud) 对于std :: priority_queue,我假设第一个模板参数指定了类型,第二个模板参数应该是该类型的容器.例:
priority_queue<int, vector<int>> someQueue;
Run Code Online (Sandbox Code Playgroud)
但是,以下代码编译并似乎运行正常:
class SomeClass
{
};
int main()
{
priority_queue <SomeClass, vector<int>> pq;
int x = 9;
pq.push(x);
int t = pq.top();
cout << t << endl;
pq.pop();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是无效的(即给出UB)?
如果它有效 - someClass在priority_queue中使用的第一个模板参数(即)是什么.