小编Gam*_*der的帖子

为什么类的字段是自动对象?

在我的studing exeption机制中,我发现在堆栈展开时会有对象字段的析构函数调用.让我明确解释一下:

class X
{
  File_ptr aa;
  Lock_ptr bb;
public:
 X(const char* x,const char* y):aa(x),bb(y){}
//.......
}
Run Code Online (Sandbox Code Playgroud)

所以,现在如果Lock_ptr的构造函数抛出一个exeption,对象aa将被销毁; 问题是"为什么"?我一直认为对象的文件不是通常的自动(lochal)对象.它们是在构造函数初始化之前创建的.所以它们在超出构造函数范围之后就不能被破坏(否则它们会被破坏)构造函数完成了它的工作)

c++ stack exe raii stack-unwinding

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

C++ 14自动扣除错误:函数返回一个数组

社区!我正在尝试应用新的C++ 14功能,并在我尝试将const char []参数传递给下面给出的函数时意外遇到错误:

 decltype(auto) autofunc( const auto& a)
 {
     cout<<"Hello World\n";
     cout<<a<<endl;

 }


 auto lambd = [](const auto& word){  autofunc(std::forward< decltype(word) > (word));};

int main()
{


 lambd("Goodbye World\n");


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

我不知道为什么,但是compilier的消息是函数试图返回一个数组(为什么会这样做?).如果我将函数的返回类型更改为void它将被编译.如果我传递另一种类型的参数(不是数组)它将被编译.数组有什么问题?

错误信息

../../untitled6/main.cpp: In instantiation of '<lambda(const auto:3&)> [with auto:3 = char [15]]':
../../untitled6/main.cpp:74:25:   required from here
../../untitled6/main.cpp:68:84: error: no matching function for call to 'autofunc(const char [15])'
  auto lambd = [](const auto& word){  autofunc(std::forward< decltype(word) > (word));};
                                                                                    ^
../../untitled6/main.cpp:68:84: note: candidate is:
../../untitled6/main.cpp:60:17: …
Run Code Online (Sandbox Code Playgroud)

c++ arrays auto c++14

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

为什么std :: forward()不推断类型?

这是下面的代码.为什么我typename remove_reference<S>::type&S&它替换它不会很好用?(我的意思是一种类型会被推断错误)

如果我传递一个rvalue(let int是int),那么S将推断为int,a's type is int&,forward()returns int&&(rvalue).

如果我传递一个lvalue(int)S将被推断为int&,as的类型是int& & ->int&,forward()返回int& &&->int&.一切顺利,为什么我们需要remove_reference呢?

template<class S>
S&& forward(typename remove_reference<S>::type& a) noexcept
{
  return static_cast<S&&>(a);
} 
Run Code Online (Sandbox Code Playgroud)

c++ reference rvalue c++11

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

标签 统计

c++ ×3

arrays ×1

auto ×1

c++11 ×1

c++14 ×1

exe ×1

raii ×1

reference ×1

rvalue ×1

stack ×1

stack-unwinding ×1