小编tom*_*sch的帖子

类似聚合的初始化,无需构造函数的多余调用

我主要尝试编写std::array. 但是,我对聚合初始化的规则(以及其他一些小细节)不太满意,因此不想使用它。

我不喜欢聚合初始化的事情:

  • 列表中少于必要的元素不会产生错误,这对于聚合的聚合来说尤其奇怪,例如
    struct A { int x,y; };
    std::array< A, 2 > arr{1,2,3};
    // produces A[0].x = 1, A[0].y = 2, A[1].x = 1, A[1].y = 0
    
    Run Code Online (Sandbox Code Playgroud)
  • 用户仍然可以调用私有/已删除的 ctor
    class B { B() = delete; };
    B{};  // no compile time error
    
    Run Code Online (Sandbox Code Playgroud)

到目前为止我所拥有的是以下内容:

#include <cstddef>
#include <type_traits>
#include <utility>

template< typename T, typename ... Ts >
inline constexpr bool areT_v = std::conjunction_v< std::is_same<T, Ts> ... >;  

namespace ttt {
    template< typename T, int N >
    struct …
Run Code Online (Sandbox Code Playgroud)

c++ aggregate-initialization c++14

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

快速整型到浮点转换

我正在使用floats 在 Cuda 中进行计算。由于 GPU 上没有足够的内存,因此我们将原始数据存储在 GPU 上uint16_tint16_t因此,在使用这些数据之前,我必须将其转换为floats。s的数量int并没有那么大(大约 12kuint16_t和 相同数量int16_t)。分析显示,转换数字需要相当长的时间(大约 5-10%)。其余的计算无法进一步优化。因此我的3+1问题是:

  • int将s 转换为s的最快方法是什么float
  • int16_t转换或.时是否有实质性差异uint16_t
  • int转换较大类型(例如int32或 )时是否存在实质性差异int64
  • 为什么 SO 上的所有问题都是关于将floats 转换为ints. 这是人们通常不会做的事情吗?

c++ floating-point integer cuda type-conversion

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

为什么禁止将 `int**const` 强制转换为 `const int**const`

允许以下情况:

    int * const p1 = nullptr;
    auto p2 = static_cast< const int * const >( p1 );
Run Code Online (Sandbox Code Playgroud)

在我的理解中p1,是一个"const-ptr to int",它被转换为一个 "const-ptr to const-int"

为什么以下内容被禁止

    int * * const pp1 = nullptr;
    auto pp2 = static_cast< const int * * const >( pp1 );
Run Code Online (Sandbox Code Playgroud)

在我的理解中,pp1const-ptr to ptr to int”应转换为“const-ptr to ptr to const-int”。因此我添加const,所以我认为这是允许的。

c++ casting const-cast

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