小编Tom*_*uwé的帖子

是否可以通过桶迭代器从std :: unordered_set中删除元素?

就像问题所说,你可以从std::unordered_set使用桶迭代器(local_iterator)中删除一个元素吗?我可以看到两种可能的解决方案

  • 由于erase()只接受全局iterators,是否有相同的功能local_iterator
  • 是否有可能获得相当于全球iteratorlocal_iterator

如果不可行,请详细说明原因.

c++ unordered-set c++11

7
推荐指数
1
解决办法
665
查看次数

模板化类中没有名为X的类模板

当试着用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似乎在类定义之外是不可见的?

c++ templates metaprogramming crtp

6
推荐指数
1
解决办法
2798
查看次数

具有独占继承的构造函数的类的值初始化

根据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{ })之前,是否应该专门提供继承的构造函数的类类型进行零初始化?

c++ value-initialization c++11

6
推荐指数
1
解决办法
184
查看次数

GCC内置矢量化类型和C数组之间有什么区别?

我有三个功能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)

assembly gcc sse vectorization

5
推荐指数
1
解决办法
714
查看次数

使用模板参数的静态成员模板

为什么这段代码不能编译?

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)

c++ templates

3
推荐指数
1
解决办法
144
查看次数