标签: c++14

为什么在定义析构函数时隐式删除​​了移动构造函数

我想知道为什么委员会决定在定义析构函数时隐式删除​​移动构造函数.

#include <iostream>
#include <vector>
#include <memory>

struct A { 
  ~A(){}; 
  std::unique_ptr<int> a;
};


int main()
{
    A a;
    A b = std::move(a);
}
Run Code Online (Sandbox Code Playgroud)

http://coliru.stacked-crooked.com/a/c0c067fc51260794

有没有任何utopic用例,这个"非默认移动成员"的规则有意义吗?

c++ c++14 c++17

0
推荐指数
2
解决办法
330
查看次数

为什么使用make_unique <>将函数传递给线程导致Valgrind中的内存泄漏?

我正在尝试这样做:

int main(){
    std::unique_ptr<std::thread> up(nullptr);
    up = std::make_unique<std::thread>(upload, url, path);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

其中,upload基本上是一个静态非成员函数,url并且pathstd::string.upload定义为:

static void upload(const string url, const string path){ int sync_status;}
Run Code Online (Sandbox Code Playgroud)

但在Valgrind下,这总是被报告为内存泄漏:

249 (80 direct, 169 indirect) bytes in 1 blocks are definitely lost in loss record 151 of 169
==12088==    at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12088==    by 0x1395F9: _S_make_state<std::thread::_Invoker<std::tuple<void (*)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >), std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> , std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> …
Run Code Online (Sandbox Code Playgroud)

c++ string multithreading c++14

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

为什么这些lambdas被认为是不同的类型?

我发现这种奇怪的行为__PRETTY_FUNCTION__,但奇怪的部分更多地与我认为是正确的行为而不是在解码中的怪癖.我错了.

auto lambda1 = [] { return __PRETTY_FUNCTION__; };
auto lambda2 = [] { return __PRETTY_FUNCTION__; };
//static_assert(std::is_same<decltype(lambda1), decltype(lambda2)>(), "Distinct types!");
std::cout << pretty(lambda1) << "\n" << pretty(lambda2) << std::endl;
lambda1(); // ...with T = main(int, const char**)::<lambda()>
lambda2(); // ...with T = main(int, const char**)::<lambda()>
Run Code Online (Sandbox Code Playgroud)

现在,我并不感到惊讶的是,他们不被认为是同一类型 - 只是他们是独特的,并没有在他们的签名中反映出来.(当lambdas返回null时,它的工作方式相同.)为什么会这样?

我还要指出,返回类型根本没有显示,我从未在常规函数中看到过.我确定返回类型是强类型的并且是lambda类型的基本部分,那么为什么它会被省略?

c++ lambda types c++14

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

将重载指针传递给成员函数信号作为QObject :: connect的参数

在Qt中,如果信号没有过载,可以像这样传递给connect方法.

QObject::connect(comboBox, &QComboBox::currentTextChanged, [&]()-> void {});
Run Code Online (Sandbox Code Playgroud)

但如果信号过载,则可以分两步完成.

例:

在Qt的QComboBox类中,突出显示的方法被重载

void QComboBox::highlighted(int index)
void QComboBox::highlighted(const QString & text)
Run Code Online (Sandbox Code Playgroud)

当使用QObject :: connect时,我们可以声明指向成员函数变量的指针,然后使用它需要2个步骤.

       void (QComboBox::*fptr) (int) = &QComboBox::highlighted;
            QObject::connect(comboBox, fptr, [&]()-> void {
                                 insertWidgetToMapAndSend(listView);
                             });
Run Code Online (Sandbox Code Playgroud)

是否可以在没有声明ftptr的情况下传递重载方法?

c++ qt c++11 c++14

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

在函数C++ 14中包含STL头文件

tl; dr:我可以以某种方式使这段代码在C++ 14(GCC 6.3)中运行吗?

int main(){
    #include<vector>
    std::vector<int> v{1,2,3};
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但下面的代码工作得很好!

#include <iostream>
using namespace std;

int main() {
    #include<cstdio>
    using namespace __gnu_cxx;
    printf("Hello world\n.");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用C++ 14(gcc-6.3)代码时不会编译错误消息

 error: 'namespace' definition is not allowed here
 namespace std
 ^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

我为什么要这样做?

我无权访问允许编码的函数.我不能在全球范围内#include.

UPD:更改为cstdlib也有效问题不是由标题保护(根据我)排除,而是名称空间问题.因为C++头文件具有命名空间std,而c头文件没有.我想问一下命名空间问题是否有一些调整?

c++ stl c++14

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

如何替换给定类型的模板参数?

考虑到一个名为模板参数B的类型是C<D>(在实例化),我怎么能构建C<A>B

这是我的代码中的最小化提取:

template<typename A>
class foo {
    template<typename B> // B is guaranteed to always be of the form C<D>
    // How to write a function here with return type C<A>?
}
Run Code Online (Sandbox Code Playgroud)

c++ templates template-meta-programming c++14

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

无法通过引用捕获lambda中的成员变量

请考虑以下代码段:

#include <map>
#include <string>
#include <set>
#include <algorithm>
#include <cmath>

using TargetsType = std::map<float, std::string>;
using TimesType = std::set<float>;

void foo (const TargetsType& targets,
          const TimesType& times)
{
    for (const auto& target : targets)
    {
        // fails to compile
        TimesType::const_iterator iter1 = std::find_if(times.begin(),
                                                       times.end(),
                                                       [&(target.first)](float item) 
                                                       {
                                                           return std::fabs(item - target.first) < 0.1f;
                                                       });

        // compiles OK
        TimesType::const_iterator iter2 = std::find_if(times.begin(),
                                                       times.end(),
                                                       [&](float item) 
                                                       {
                                                           return std::fabs(item - target.first) < 0.1f;
                                                       });
    }
}
Run Code Online (Sandbox Code Playgroud)

iter1无法编译的声明,出现以下错误:

error: expected ',' …
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11 c++14

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

当lambda执行时,C++ lambda捕获的引用具有不同的值

我正在为一个项目构建一个pubsub系统,我有一个有趣的问题,事件值在通知和事件发送之间变化.我遇到问题的事件是一个MouseMoveEvent,其中包含使用glm :: vec2()的屏幕坐标(它有双重类型的x和y字段).

template<class E>
void notify_event(std::shared_ptr<E> event) {
  if (_auto_dispatch) {
    dispatch<E>(event);
  } else {
    // inspecting values here gives expected coordinates: 
    // like: 168.684,284.547
    debug<E>(event); 
    _dispatch_queue.push([&]() {
      // when the lambda runs, the coordinates are major messed up
      // like: -1.82767e+16,4.57398e-41
      debug<E>(event); 
      dispatch<E>(event);
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

无论输入值如何,都给出-1.82767e + 16,4.57398e-41坐标.我最初使用引用传递事件,然后尝试共享指针,但我得到两个完全相同的结果.调度事件立即运行没有任何问题,但我的项目要求我收集事件,然后稍后调度,因为不同的系统将在创建事件时处于"准备好"的各种状态.

我觉得捕获的事件引用最终以某种方式指向垃圾内存,但我不太清楚C++中的lambdas,知道我为什么会看到它.

发生了什么事,有什么选择可以解决这个问题?

c++ c++14

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

vector push_back()给出了编译器错误C2280

我试图将自定义类对象附加到相同类型的向量,但是当我尝试编译我的代码时,编译器给出以下错误

gltf::gltf(const gltf &): attempting to reference a deleted function

我的函数接受一个字符串(文件名)作为参数并加载该文件,然后解析该文件并填充它的变量

功能如下: -

void enigma::loadModel(std::string file)
{       

    gltf stagingModel;
    stagingModel.loadAsset(file);   // populates my object with data
    model.push_back(stagingModel);  // appends the populated object to a vector array (this line generates the error)

    ....   // the rest of the code
}
Run Code Online (Sandbox Code Playgroud)

我的gltf类decleration如下: -

#pragma once
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include "meshClass.h"
#include "bufferClass.h"
#include <gtx/quaternion.hpp>
#include <gtx/transform.hpp>

#include"stagingNodeClass.h"
#include"stagingMeshClass.h"
#include"stagingAccessorClass.h"
#include"stagingBufferViewClass.h"
#include"stagingImageClass.h"
#include"stagingSamplerClass.h"
#include"stagingTextureClass.h"
#include"stagingMaterialClass.h"
#include"stagingSkinClass.h"

class gltf …
Run Code Online (Sandbox Code Playgroud)

c++ vector c++11 c++14

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

在方法中填充的地图的shared_ptr在调用者中返回空

我正在初始化一个shared_ptr以映射到一个名为GetData的单独函数中.此映射作为参数传递给GetData函数.但是,在main中,map在调用GetData函数后返回空.

#include <memory>
#include <map>

void  GetData(std::shared_ptr<std::map<int, int>> data);

int main()
{
    std::shared_ptr<std::map<int, int>> data2 = std::make_shared<std::map<int, int>>();
    GetData(data2);
    return 0;
}

void GetData(std::shared_ptr<std::map<int, int>> data)
{

    data = std::make_shared<std::map<int, int>>
        (std::initializer_list<std::map<int, int>::value_type>{
            { 1, 2 },
            { 5, 6 },
            { 4, 5 }
    });
}
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

更新: 如果在参数仍未通过引用传递的情况下重写方法如下,我确实在调用GetData方法后在main中填充了map.

void GetData(std::shared_ptr<std::map<int, int>> data)
{
    data->insert(std::pair<int, int>(1,2));
    data->insert(std::pair<int, int>(45, 2));
}
Run Code Online (Sandbox Code Playgroud)

c++ stdmap shared-ptr c++14

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