小编son*_*yao的帖子

从已捕获的lambda中反复移动变量

我有以下测试代码:

#include <iostream>
#include <string>

void printValue(std::string&& val)
{
    std::cout << "Got value: " << val << "\n";
}

int main() {

    std::string testValue = "Test Value";

    auto lambda = [testValue = std::move(testValue)]() mutable
    {
        printValue(std::move(testValue));
    };

    lambda();
    lambda();
    lambda();
}
Run Code Online (Sandbox Code Playgroud)

我得到了结果:

Got value: Test Value
Got value: Test Value
Got value: Test Value
Run Code Online (Sandbox Code Playgroud)

它是一个有效的假设,从已经移动捕获的lambda中移动一个对象将始终具有它被移动到lambda中的初始状态,或者这只是一个对象处于"有效但未指定状态"的工件?

c++ lambda move-semantics c++11 c++14

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

为什么sfinae超载没有解决

这个版本工作正常:

template<typename T>
struct Foo
{
    template<typename U = T>
        typename std::enable_if<std::is_same<U,A>::value>::type
        bar() { std::cout << "1" << std::endl; }

    template<typename U = T> 
        typename std::enable_if<std::is_same<U,B>::value>::type
        bar() { std::cout << "2" << std::endl; }
};  
Run Code Online (Sandbox Code Playgroud)

此版本失败:

template<typename T>
struct Foo2
{
    template<typename U = T, typename V = typename std::enable_if<std::is_same<U,A>::value>::type >
        V bar() { std::cout << "1" << std::endl; }

    template<typename U = T, typename V = typename std::enable_if<std::is_same<U,B>::value>::type >
        V bar() { std::cout << "2" << std::endl; }
}; …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading sfinae

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

C++:将unique_ptr :: get()之类的参数传递给函数是否安全

传递函数参数是否安全getAName(getA().get())getA()返回一个对象unique_ptr<A>.

我在VS 2010上测试下面的全部代码,它可以工作.但我想确定它是否是c ++标准,对其他c ++编译器是否安全?

#include "stdafx.h"
#include <memory>
#include <iostream>

using namespace std;

class A
{
public:
    A(){ cout<<"A()"<<endl;}
    ~A(){ cout<<"~A()"<<endl;}

    string name() { return "A"; }
};

std::unique_ptr<A> getA()
{
    return std::unique_ptr<A>(new A());;
}

void getAName(A* a)
{
    if(a)
    {
        cout << a->name().c_str() << endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    getAName(getA().get());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

控制台中的输出是:

A()
A
~()
Run Code Online (Sandbox Code Playgroud)

为了所有编译器的安全,是否有必要使代码如下所示?

unique_ptr<A> a = getA();
getAName(a.get());
Run Code Online (Sandbox Code Playgroud)

c++ stl temporary unique-ptr c++11

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

当移动和复制构造函数存在时,C++默认构造函数不会使用"using"继承

class A{

public:
    A(){};

};

class B : public A{

public:
    using A::A;

    B(const B&) =   default;
    B(      B&&) =  default;

};

B b;
Run Code Online (Sandbox Code Playgroud)

编译器(g ++(5.4.0-6ubuntu1)/ c ++ 11)说"没有用于调用B :: B()的匹配函数",并将复制和移动构造函数列为候选.如果我评论那些默认的那些,那么它就会编译.是什么导致这个?它们明显违约有什么区别?如果那两条线不在那里,他们无论如何都会被拖欠.

c++ constructor language-lawyer c++11 c++17

9
推荐指数
2
解决办法
1080
查看次数

迭代取消引用的unique_ptr,包含vector,用于范围循环

为什么这段代码不像我想的那样工作?

for (auto it: *std::make_unique<std::vector<int>>(std::vector<int>({1, 2, 3, 4, 5})))
    std::cout << it << std::endl;
Run Code Online (Sandbox Code Playgroud)

在执行循环的第一次迭代之前,矢量对象被销毁

c++ for-loop vector unique-ptr c++11

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

非命名空间作用域中的显式特化不在GCC中编译

以下代码在Clang中编译,但在GCC中不编译:

template<typename T>
struct Widget
{
    template<typename U>
    void foo(U)
    {
    }

    template<>
    void foo(int*)
    {
    }
};
Run Code Online (Sandbox Code Playgroud)

根据C++标准([temp.expl.spec],第2段):

可以在可以定义相应主模板的任何范围中声明显式特化

这是GCC中的一个错误,如果是这样,我怎样才能在它的bug追踪器中找到它?

这是GCC的输出:

prog.cc:13:14: error: explicit specialization in non-namespace scope 'struct Widget<T>'
     template<>
              ^
Run Code Online (Sandbox Code Playgroud)

我正在使用GCC HEAD 8.0.1 -std=c++2a.

c++ gcc templates bug-reporting language-lawyer

9
推荐指数
2
解决办法
1950
查看次数

如何在不指定数组大小的情况下声明数组,而在C ++中的类内声明一个初始化器?

如果它具有初始化程序,则可以在不显式说明其大小的情况下声明数组:

// very fine: decltype(nums) is deduced to be int[3]
int nums[] = { 5, 4, 3 }; 
Run Code Online (Sandbox Code Playgroud)

但是,当在类中声明数组时,这是行不通的:

class dummy_class
{
    // incomplete type is not allowed (VS 2019 c++17)
    int nums[] = { 5, 4, 3 }; 
};
Run Code Online (Sandbox Code Playgroud)

为什么会这样呢?

c++ arrays language-lawyer c++17

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

operator new 的执行顺序和构造函数的参数

C++ 规范是否指定了in的顺序operator new和构造函数。 g++ 让顺序为-> -> ,但 clang++ 让它为-> -> 。 差异是由未指定的行为引起的吗?Anew C(A())
A()newC()newA()C()

g++:7.4.0 叮当++:10.0.0

#include <iostream>
#include <cstdlib>

struct A {
    A() {
        std::cout << "call A()\n";
    }
};

struct C {
    C(A) {
        std::cout << "call S()\n";
    }

    void *operator new(size_t s) {
        std::cout << "call new()\n";
        return malloc(s);
    }
};

int main() {
    void *p = new C(A());
}
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer order-of-execution c++17

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

隐藏用于聚合初始化的空基类

考虑以下代码:

struct A
{
    // No data members
    //...
};

template<typename T, size_t N>
struct B : A
{
    T data[N];
}
Run Code Online (Sandbox Code Playgroud)

这就是你必须如何初始化 B:B<int, 3> b = { {}, {1, 2, 3} }; 我想避免基类不必要的空 {}。Jarod42 here提出了一个解决方案,但是,它不适用于元素默认初始化:B<int, 3> b = {1, 2, 3};很好,但B<int, 3> b = {1};不是:b.data[1]并且b.data[2]没有默认初始化为 0,并且会发生编译器错误。有什么方法(或者 C++20 会有)从构造中“隐藏”基类?

c++ initialization aggregate-initialization c++20

9
推荐指数
2
解决办法
445
查看次数

为什么在 C++ 中 true &amp;&amp; false 等于 1?

我在玩布尔值,最后得到了这行代码:

std::cout << true && false;

由于某种原因,它产生1. 这怎么可能,如果&&要求双方都为真,才能产生1

c++ boolean-logic boolean operator-precedence logical-operators

9
推荐指数
2
解决办法
251
查看次数