小编fin*_*ncs的帖子

如何使用G ++抑制纯虚拟类的C++ vtable生成?

可以使用该__declspec(novtable)属性在MSVC中完成抑制C++ vtable生成.但是,似乎GNU C++编译器没有等效属性.事实上,将vtable留给纯虚拟类不必要地链接到__cxa_abort()许多其他类,我想避免这种情况发生,因为我正在为嵌入式系统编程.所以我该怎么做?

struct ISomeInterface
{
    virtual void Func() = 0;
};

class CSomeClass : public ISomeInterface
{
    virtual void Func();
}

void CSomeClass::Func()
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

c++ gcc virtual-functions g++ vtable

8
推荐指数
1
解决办法
1968
查看次数

C++函数模板参数与模板化类型struct woes

这段代码无法解释:

struct sometype
{
    template <typename T>
    T* get() { return nullptr; }
};

template <typename T>
struct anothertype
{
#if 1
    template <typename T2> struct some_wrapper { typedef T2 type; };
    typedef typename some_wrapper<sometype>::type thetype;
#else
    typedef sometype thetype;
#endif
    typedef thetype* Ptr;

    Ptr m_ptr;
    T* get() { return m_ptr->get<T>(); }
};
Run Code Online (Sandbox Code Playgroud)

如果我将#if参数更改为0,则会以某种方式修复.有人可以解释一下吗?请注意,显然毫无意义的some_wrapper事情实际上在我的真实代码中做了一些有用的事情.

我正在使用g ++ 4.7.1 -fstd=gnu++11,错误如下(指向我声明的行anothertype<T>::get:

error: expected primary-expression before '>' token
error: expected primary-expression before ')' token
Run Code Online (Sandbox Code Playgroud)

c++ templates arguments function c++11

6
推荐指数
2
解决办法
245
查看次数

C++拷贝初始化+隐式构造函数call = fail

这段代码:

class foo
{
    int x;
public:
    foo(int x) : x(x) { }
    int get() const { return x; }
    //...
};

class bar
{
    int x;
public:
    bar(const foo& x) : x(x.get()) { }
    int get() const { return x; }
    bar& operator =(const foo& rhs) { x = rhs.get(); return *this; }
    //...
};

void func()
{
    foo f = 3;
    bar b = 3;
    b = 7;
    //...
}
Run Code Online (Sandbox Code Playgroud)

在线上出错bar b = 3(g ++ 4.7.1 …

c++ constructor implicit-conversion copy-initialization c++11

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