就像问题所说,你可以从std::unordered_set
使用桶迭代器(local_iterator
)中删除一个元素吗?我可以看到两种可能的解决方案
iterator
s,是否有相同的功能local_iterator
?iterator
的local_iterator
?如果不可行,请详细说明原因.
当试着用GCC 4.6.0编译这个(类似CRTP的)代码时:
template<template<class> class T> struct A;
template<class T>
struct B: A<B<T>::template X> {
template <class U> struct X { U mem; };
};
B<int> a;
Run Code Online (Sandbox Code Playgroud)
我得到错误消息"test.cpp:3:26:错误:'struct B <int>'中没有名为'X'的类模板".为什么X似乎在类定义之外是不可见的?
根据cppreference,没有任何用户提供的构造函数的非联合类类型在构造之前将被初始化为零:
如果T是没有任何用户提供的构造函数的非联合类类型,则该对象被零初始化,然后调用隐式声明的默认构造函数(除非它是微不足道的)
我不确定当使用c ++ 11继承的构造函数时会发生什么,因为引用显式地提到了隐式声明的默认构造函数.
给出以下示例:
#include <iostream>
struct A {
int a;
A() {}
A(int i): a(i) {}
};
struct B: public A {
using A::A;
};
int main() {
B b { 5 };
B* p = new (&b) B{ };
std::cout << b.a << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
什么是正确的输出,0或5?在value-initialization(B{ }
)之前,是否应该专门提供继承的构造函数的类类型进行零初始化?
我有三个功能a()
,b()
和c()
那些应该做同样的事情:
typedef float Builtin __attribute__ ((vector_size (16)));
typedef struct {
float values[4];
} Struct;
typedef union {
Builtin b;
Struct s;
} Union;
extern void printv(Builtin);
extern void printv(Union);
extern void printv(Struct);
int a() {
Builtin m = { 1.0, 2.0, 3.0, 4.0 };
printv(m);
}
int b() {
Union m = { 1.0, 2.0, 3.0, 4.0 };
printv(m);
}
int c() {
Struct m = { 1.0, 2.0, 3.0, 4.0 };
printv(m);
} …
Run Code Online (Sandbox Code Playgroud) 为什么这段代码不能编译?
struct A {
template <class T>
static T a(int i) { return 2*i; }
};
template <class T>
struct B {
double b;
B(): b(T::a<double>(5)) {}
};
template class B<A>;
Run Code Online (Sandbox Code Playgroud)
编译器甚至没有到达模板实例化.我正在使用gcc 4.7.0.
test.cc: In constructor »B<T>::B()«:
test.cc:9:25: Error: expected »(« before »<« token
test.cc:9:26: Error: expected primary-expression before »double«
Run Code Online (Sandbox Code Playgroud)