小编Cos*_*ana的帖子

使用T []作为模板参数

我最近偶然发现了使用unique_ptr<T[]>,我明白的目的是删除指针delete[].

令我困惑的unique_ptr<T[3]>是相反无效(如果我错了,请纠正我).

模板中T []的类型是什么?它与T [3]有什么不同?这些是数组,所以它们不应该是一样的吗?T []作为模板中的类型还有其他用途吗?

c++

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

编写饱和的转换运算符而不列出所有可能的组合

我想在不同类型之间创建模板化操作(假设这是列表:int8_t,int16_t,int32_t,int64_t,uint8_t,uint16_t,uint32_t,uint64_t,float,double)。

然后,我想让一个saturate_cast<>()函数接受输入值,检查它是否在输出类型限制之内,并在需要时饱和到那些限制。

问题是如果我加两个int32_t,默认的C ++操作在溢出的情况下具有未定义的行为,因此我想将该操作提升为int64_t并使用该类型来执行该操作。

临时解决方案可以是:

#include <cstdint>
#include <limits>

template<typename T1, typename T2> struct type_which_fits { using type = decltype(T1() + T2()); };
template<> struct type_which_fits<int32_t, int32_t> { using type = int64_t; };

template<typename T1, typename T2>
auto TAdd(T1 lhs, T2 rhs) {
    using type = typename type_which_fits<T1, T2>::type;
    return static_cast<type>(lhs) + static_cast<type>(rhs);
}

template<typename ODT, typename IDT>
ODT saturate_cast(const IDT& v) {
    if (v > std::numeric_limits<ODT>::max()) {
        return std::numeric_limits<ODT>::max();
    }
    if (v < …
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization

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

使用`size_t`表示长度会影响编译器优化?

在阅读这个问题时,我看到第一条评论说:

size_t 对于长度不是一个好主意,正确的类型是签名的优化/ UB原因.

其次是支持推理的另一条评论.这是真的吗?

这个问题很重要,因为如果我要写一个矩阵库,图像尺寸可能是size_t,只是为了避免检查它们是否为负数.但是所有循环都会自然而然地使用size_t.这会对优化产生影响吗?

c++ compiler-optimization

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

使用 std::istream::peek() 总是安全的吗?

我通常教我的学生处理文件输入的安全方法是:

while (true) {
    // Try to read
    if (/* failure check */) {
        break;
    }
    // Use what you read
}
Run Code Online (Sandbox Code Playgroud)

这使我和许多人免于经典和大部分时间错误:

while (!is.eof()) {
    // Try to read
    // Use what you read
}
Run Code Online (Sandbox Code Playgroud)

但是人们真的很喜欢这种形式的循环,因此在学生代码中看到这种形式变得很常见:

while (is.peek()!=EOF) { // <-- I know this is not C++ style, but this is how it is usually written
    // Try to read
    // Use what you read
}
Run Code Online (Sandbox Code Playgroud)

现在的问题是:这段代码有问题吗?是否存在某些情况无法完全按预期工作的极端情况?好的,这是两个问题。

编辑其他详细信息:在考试期间,您有时会向学生保证文件的格式正确,因此他们无需进行所有检查,只需验证是否有更多数据。大多数时候我们处理二进制格式,这让您完全不用担心空格(因为数据都是有意义的)。

虽然接受的答案是完全清楚的,正确的,我还是喜欢一个人,试图评论的联合行为peek()unget()

unget()我想到了这些东西,因为我曾经观察到(我相信它是在 Windows 上)通过查看 4096 …

c++ istream

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

VS2017中缺少Win32控制台应用程序.如何创建C++空项目?

我安装了VS2017 community,我迷路了:Win32控制台应用程序丢失了.

我去的时候甚至没有模板New Project,也无法创建C++空项目VS2017.

我怎么解决呢?

c++ visual-studio visual-studio-2017

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

有时我的二进制信号量没有等待正确的时间

有时它等待的时间不够长。我可能错过了一些简单的东西 - 但我找不到它。为什么等待函数有时会过早返回

#define SEMAPHORE_MAXWAIT   -1
#define SEMAPHORE_NOWAIT    0

// Type your code here, or load an example.
typedef struct binary_semaphore {
    pthread_mutex_t mutex;
    pthread_cond_t condvar;
    bool variable;
}binary_semaphore_t;


static struct timespec *timespec_addms(struct timespec *ts, unsigned ms)
{
    uint64_t nsec;
    if(ts)
    {
        ts -> tv_sec += ms / 1000;
        nsec = ts -> tv_nsec + (ms % 1000) * 1000000ULL;
        ts -> tv_sec += nsec / 1000000000ULL;
        ts -> tv_nsec = nsec % 1000000000ULL;
    }
    return ts;
}

static int …
Run Code Online (Sandbox Code Playgroud)

c mutex timeout semaphore pthreads

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