gpt4 book ai didi

c++ - 通过 ctor 右值参数的 Lambda 赋值

转载 作者:太空狗 更新时间:2023-10-29 21:04:00 26 4
gpt4 key购买 nike

从代码项目中获取此 ScopeExit 类,但它不会构建在 GCC 4.5.3 上。感谢任何帮助。

class ScopeExit : private boost::noncopyable
{
typedef std::function<void()> func_t;

public:
ScopeExit(func_t&& f) : func(f) {}
~ScopeExit() { func(); }

private:
// no default ctor
ScopeExit();

// Prohibit construction from lvalues.
ScopeExit(func_t&);

// Prohibit new/delete.
void* operator new(size_t);
void* operator new[](size_t);
void operator delete(void *);
void operator delete[](void *);

const func_t func;
};


ScopeExit exit = [&]() { };

gcc 4.5.3 错误:

In member function ‘void test()’:
error: conversion from ‘test()::<lambda()>’ to non-scalar type ‘ScopeExit’ requested

编辑:

ScopeExit exit([&]() { }); // this works

最佳答案

这是复制/移动初始化。你的copy c-tor被删除了,move c-tor也被删除了。

n3337 12.8/9

If the definition of a class X does not explicitly declare a move constructor, one will be implicitly declaredas defaulted if and only if

— X does not have a user-declared copy constructor,

— X does not have a user-declared copy assignment operator,

— X does not have a user-declared move assignment operator,

— X does not have a user-declared destructor, and

— the move constructor would not be implicitly defined as deleted.

不知道为什么第一种情况不起作用,但这种情况很好

template<typename T>
ScopeExit(T&& f) : func(std::move(f)) {}
ScopeExit(ScopeExit&& rhs) : func(std::move(rhs.func)) { }]

编辑。

当我们使用类类型变量的复制初始化时,仅使用标准和省略隐式转换。从 lambda 到函数指针或从函数指针到 std::function 的转换是用户定义的转换,未使用。

n3337 8.5/16

The semantics of initializers are as follows. The destination type is the type of the object or reference beinginitialized and the source type is the type of the initializer expression. If the initializer is not a single (possiblyparenthesized) expression, the source type is not defined.

If the destination type is a (possibly cv-qualified) class type:

Otherwise (i.e., for the remaining copy-initialization cases), user-defined conversion sequencesthat can convert from the source type to the destination type or (when a conversion functionis used) to a derived class thereof are enumerated as described in 13.3.1.4, and the best one ischosen through overload resolution (13.3). If the conversion cannot be done or is ambiguous, theinitialization is ill-formed.

n3337 13.3.1.4/1

Under the conditions specified in 8.5, as part of a copy-initialization of an object of class type, a user-defined conversion can be invoked to convert an initializer expression to the type of the object being initialized.Overload resolution is used to select the user-defined conversion to be invoked. Assuming that “cv1 T” isthe type of the object being initialized, with T a class type, the candidate functions are selected as follows:

— The converting constructors (12.3.1) of T are candidate functions.

  1. In both cases, the argument list has one argument, which is the initializer expression. [ Note: This argumentwill be compared against the first parameter of the constructors and against the implicit object parameterof the conversion functions. — end note ]

n3337 13.3.2

1. From the set of candidate functions constructed for a given context (13.3.1), a set of viable functions ischosen, from which the best function will be selected by comparing argument conversion sequences for thebest fit (13.3.3). The selection of viable functions considers relationships between arguments and functionparameters other than the ranking of conversion sequences.

Second, for F to be a viable function, there shall exist for each argument an implicit conversion se-quence (13.3.3.1) that converts that argument to the corresponding parameter of F.

n3337 13.3.3.1/4

However, when considering the argument of a constructor or user-defined conversion function that is acandidate by 13.3.1.3 when invoked for the copying/moving of the temporary in the second step of a classcopy-initialization, by 13.3.1.7 when passing the initializer list as a single argument or when the initializerlist has exactly one element and a conversion to some class X or reference to (possibly cv-qualified) X isconsidered for the first parameter of a constructor of X, or by 13.3.1.4, 13.3.1.5, or 13.3.1.6 in all cases, onlystandard conversion sequences and ellipsis conversion sequences are considered.

关于c++ - 通过 ctor 右值参数的 Lambda 赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12471863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com