小编Pet*_*lev的帖子

隐藏内部类型

我想隐藏库用户的内部类型.
目前我有这样的事情:

foo.h中

typedef struct public
{
    uint16 a;
    //...
    unsigned char internals[4];
} public_type;
Run Code Online (Sandbox Code Playgroud)

foo.c的

typedef struct public
{
    uint32_t a;
}internals_type;
Run Code Online (Sandbox Code Playgroud)

然后在函数中,我正在做一个类似的演员.

void bar(public_type * const public_struct)
{
     internals_type* const internals = &public_struct->internals;
     intrnals->a = getSomething();
     // .....
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法呢?

我已经尝试了一些奇怪的东西与标题中的联合和指针,但没有什么似乎更好,我很好奇,如果这可以得到任何更清洁或至少如果从一种类型指针指向另一种指针的警告可以被删除.

c embedded

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

现代C++中std :: bind的替代方案

所以我正在尝试创建一个具有不同类型的仿函数容器的类.

这是它的简化版本.

template<class T>
class Container
{

public:
    template<typename F, typename ... ARGS>
    void addTask(F && func, ARGS && ... args);
private:
    std::deque<std::function<T()>> container;

    //.....
};

template<class T>
template<typename F, typename ... ARGS>
T Container<T>::addTask(F && func, ARGS && ... args);
{
    container.emplace_back(std::bind(f,args...));

    //.....
}
Run Code Online (Sandbox Code Playgroud)

还有一些我无法解决的问题.

  1. 有没有办法删除std::bind和存储不同的对象或指针?
  2. 这可能更通用吗?我可以以某种方式存储在单个容器(int,void...)中返回不同对象的函数吗?
  3. 可以在编译时执行创建任务的一些逻辑吗?像consexprbind 这样的东西.

c++ c++11 c++14 c++17

7
推荐指数
2
解决办法
2085
查看次数

Constexpr用于创建对象

我试图弄清楚是否有通过constexpr而不是正常创建对象的性能提升.

这是代码片段constexpr.

class Rect
{
    const int a;
    const float b;
public:
    constexpr Rect(const int a,const float b)
    : a(a),b(b){}
};

int main()
{
     constexpr Rect rect = Rect(1,2.0f);
}
Run Code Online (Sandbox Code Playgroud)

没有constexpr.

class Rect
{
    int a;
    float b;
public:
    Rect(int a, float b)
    : a(a),b(b){}
};

int main()
{
    Rect rect = Rect(1,2.0f);
}
Run Code Online (Sandbox Code Playgroud)

constexpr因为内存应该在编译时初始化,所以我期待会有更少的代码.

我使用constexpr得当吗?如果不是这样,你可以使用constexpr在编译时创建对象,然后在没有任何运行时开销的情况下使用它们吗?

谢谢!

c++ constexpr

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

模板功能专用默认

我正在尝试创建一个可以使用特定数据类型的类,并且我希望在不支持数据类型时出现编译时错误.

我试图专门化这样的模板.

template<>
float Foo::get<float>(const std::string& key)
{
    return std::stof(key);
}
Run Code Online (Sandbox Code Playgroud)

std::static_assert在通用函数中添加一个因为我只需要指定的这些类型.

template<class T>
T Foo::get(const std::string&)
{
    static_assert(false, "unsupported data type");
    return T(0);
}
Run Code Online (Sandbox Code Playgroud)

不幸的是我得到了编译错误(静态断言失败),即使我有这种类型的专用函数.

我找到了一种方法只为特定类型做这件事,但它看起来有点愚蠢而且不通用.

T Foo::get(const std::string&)
{
    static_assert(
            std::is_same<T,float>::value ||
            std::is_same<T,double>::value ||
            std::is_same<T,bool>::value ||
            std::is_same<T,uint32_t>::value ||
            std::is_same<T,int32_t>::value ||
            std::is_same<T,std::string>::value,
            "unsuported data type");
    return T(0);
}
Run Code Online (Sandbox Code Playgroud)

c++ templates compiler-errors c++11

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

标签 统计

c++ ×3

c++11 ×2

c ×1

c++14 ×1

c++17 ×1

compiler-errors ×1

constexpr ×1

embedded ×1

templates ×1