小编Dan*_*rey的帖子

为什么C++ 14 Generic lambdas需要自动参数规范?

我看看Generic lambdas并且无法得到它 - 为什么要保留类型声明?为什么不 (x, y)呢?如果编译器供应商必须支持(auto a, auto b)是否有任何支持简单的问题(a, b)

c++ lambda c++14

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

C++ 11中的char16_t和char32_t类型

据我了解,在C++ 11 char16_tchar32_t不是类型定义,但新的基本数据类型.(如果我错了,请纠正我).

我正在编写一个用于linux gcc和Windows Visual Studio的库.所以我写了一个特点:

template <class T>
struct IsChar : std::false_type {
};

template <>
struct IsChar<char> : std::true_type {
};

template <>
struct IsChar<wchar_t> : std::true_type {
};

template <>
struct IsChar<char16_t> : std::true_type {
};

template <>
struct IsChar<char32_t> : std::true_type {
};
Run Code Online (Sandbox Code Playgroud)

然而,我注意到,在VS(2013),char16_tchar32_t仅仅类型定义为unsigned shortunsigned[..]\Microsoft Visual Studio 12.0\VC\include\yvals.h.

所以我查了一下

std::is_same<char32_t, unsigned>::value

true在VS和false海湾合作委员会.

所以我的问题是:

  1. char16_tchar32_t标准中的新原始类型(这两种类型的VS实现是非标准的吗?) …

c++ gcc c++11 visual-studio-2013

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

判断两个表是否有相同的键集

我将 Lua 表用作集合,将集合的值放入表键中并1作为表值,例如

function addToSet(s,...)      for _,e in ipairs{...} do s[e]=1   end end
function removeFromSet(s,...) for _,e in ipairs{...} do s[e]=nil end end

local logics = {}
addToSet(logics,true,false,"maybe")
Run Code Online (Sandbox Code Playgroud)

为了测试两组是否相等,我需要确保它们具有完全相同的键。什么是有效的方法来做到这一点?

performance lua set

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

c ++继承可见性模式

继承期间类的默认可见性模式是什么(此处为D @ class中的B)

class B {
public:
    int key;
    B(void) { key = 0; printf("B constructed\n");}
    virtual void Tell(void);
    ~B(void) {cout <<"B destroyed"<<endl << endl;}
};


class D2 : B {
public:
    void Tell(void) { printf("D2 Here\n"); }
};
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

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

std :: chrono:将时钟的纪元设置为1/1/0000

是否可以手动将纪元日期/时间设置为0000年1月1日,因此我可以使用std :: chrono :: time_point :: time_since_epoch来计算给定日期与1月1日0000之间的差异?

我尝试了以下方法:

#include <iostream>
#include <chrono>
#include <ctime>

int main(int argc, char*argv[])
{
    std::tm epochStart = {};
    epochStart.tm_sec = 0;
    epochStart.tm_min = 0;
    epochStart.tm_hour = 0;
    epochStart.tm_mday = 0;
    epochStart.tm_mon = 0;
    epochStart.tm_year = -1900;
    epochStart.tm_wday = 0;
    epochStart.tm_yday = 0;
    epochStart.tm_isdst = -1;

    std::time_t base = std::mktime(&epochStart);

    std::chrono::system_clock::time_point baseTp=
        std::chrono::system_clock::from_time_t(base);
    std::time_t btp = std::chrono::system_clock::to_time_t(baseTp);
    std::cout << "time: " << std::ctime(&btp);

}
Run Code Online (Sandbox Code Playgroud)

但这给了我

time: Thu Jan  1 00:59:59 1970
Run Code Online (Sandbox Code Playgroud)

c++ epoch c++11 c++-chrono

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

在C++ 11中释放动态分配的uv_timer_t(libuv)实例

我有一个需要将任务安排到libuv事件循环的函数.我的想法是创建一个0ms超时的计时器.我试过以下代码:

void myFunction() {
    ...
    uv_timer_t* timer = new uv_timer_t();
    uv_timer_init(uv_default_loop(), timer);
    uv_timer_start(timer, [&](uv_timer_t* timer, int status) {
        // Scheduled task
    }, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

这种方法运行良好但问题是,动态分配的计时器永远不会被释放.我已经尝试在回调中释放计时器,但这导致了分段错误:

void myFunction() {
    ...
    uv_timer_t* timer = new uv_timer_t();
    uv_timer_init(uv_default_loop(), timer);
    uv_timer_start(timer, [&](uv_timer_t* timer, int status) {
        // Scheduled task
        delete timer;
    }, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过调用uv_timer_stop(timer);uv_unref((uv_handle_t*) timer);在实际内存释放之前,但是分段错误仍然是remian.

c++ memory-leaks event-loop dynamic-allocation c++11

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

将逗号分隔的参数传递给<<运算符

我正在尝试Eigen为容器类完成类似于高级初始化的操作.

即在main,我想填写类型的对象DynamicArray如下:

main.cpp中

// Create 3 x 3 Dynamic array and fill with 0's
DynamicArray da(3, 3, 0);

da << 1, 2, 3,
      4, 5, 6,
      7, 8, 9;
Run Code Online (Sandbox Code Playgroud)

我用来尝试实现此目的的方法是:

DynamicArray.hpp

template <typename T>
class DynamicArray {

        // ...

public:

        // ...

    template <typename... Args,
              typename = typename std::enable_if<ArgPackSameTruth<
                                             std::is_convertible<Args, T>...>::value,
                                             T>::type
    > void operator<<(Args... x) {

        typename std::vector<T> arg_vect{std::forward<Args>(x)...};

        std::cout << arg_vect.size() << "\n";

        // Load contents of arg_vect into *this...

        return;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading c++11

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

在析构函数中访问违反"删除"指针

我的二进制搜索树析构函数看起来像这样.

~BSTree()
{
    if (this == nullptr || this->left == nullptr && this->right == nullptr)
    {
        return;
    }
    this->left->~BSTree();
    delete this->left;
    this->right->~BSTree();
    delete this->right;
}
Run Code Online (Sandbox Code Playgroud)

调用堆栈获取有关> = 4后叫我的程序崩溃在if()访问Voilation例外.

我的领域是只有三个:int key;,BSTree *left;BSTree *right;

在此输入图像描述

它似乎this不是NULL但它的字段无法从内存中读取.如何检查是否可以,remove this;如果不能防止异常?

c++ memory exception access-violation c++11

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