#include <iostream>
#include <string>
typedef std::string S;
template <typename T>
static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
bool assigned = false;
if (!assigned) {
// invoke creationSpren with passed arguments
// assign
}
}
int main()
{
auto& xx = []() {
return new std::string("abc");
};
auto& zzz = getOrCreate<S>(xx);
}
注意:这段代码不能编译,这是我要解决的问题。
然而,我写了这个最小的例子来说明这个问题,它尽可能的简单。
我尝试实现的很简单,就是使用lambda来实现对象的惰性初始化,当需要的时候(即当检索失败时,它调用lambda并分配对象(即存储它)并返回它)
我有什么问题,因为我没有经验与lambdas是这两个签名。
这就是我要问的,如何写2个lambda签名。谢谢。
是的,它需要模板。
逐字错误
<source>: In lambda function:
<source>:7:45: error: expected '{' before ')' token
7 | static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
| ^
<source>: At global scope:
<source>:7:46: error: expected ')' before 'creationSpren'
7 | static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
| ~ ^~~~~~~~~~~~~~
| )
<source>:7:63: error: expected ';' before '{' token
7 | static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
| ^~
| ;
<source>: In function 'int main()':
<source>:18:16: error: cannot bind non-const lvalue reference of type 'main()::<lambda()>&' to an rvalue of type 'main()::<lambda()>'
18 | auto& xx = []() {
| ^~~~~~
19 | return new std::string("abc");
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 | };
| ~
<source>: In instantiation of 'std::__cxx11::basic_string<char>* getOrCreate<std::__cxx11::basic_string<char> >':
<source>:22:17: required from here
<source>:7:33: error: cannot convert '<lambda()>' to 'std::__cxx11::basic_string<char>*' in initialization
7 | static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
| ^~~~~~~~~~~~
| |
| <lambda()>
<source>:22:31: error: 'getOrCreate<std::__cxx11::basic_string<char> >' cannot be used as a function
22 | auto& zzz = getOrCreate<S>(xx);
| ~~~~~~~~~~~~~~^~~~
ASM generation compiler returned: 1
<source>: In lambda function:
<source>:7:45: error: expected '{' before ')' token
7 | static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
| ^
<source>: At global scope:
<source>:7:46: error: expected ')' before 'creationSpren'
7 | static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
| ~ ^~~~~~~~~~~~~~
| )
<source>:7:63: error: expected ';' before '{' token
7 | static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
| ^~
| ;
<source>: In function 'int main()':
<source>:18:16: error: cannot bind non-const lvalue reference of type 'main()::<lambda()>&' to an rvalue of type 'main()::<lambda()>'
18 | auto& xx = []() {
| ^~~~~~
19 | return new std::string("abc");
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 | };
| ~
<source>: In instantiation of 'std::__cxx11::basic_string<char>* getOrCreate<std::__cxx11::basic_string<char> >':
<source>:22:17: required from here
<source>:7:33: error: cannot convert '<lambda()>' to 'std::__cxx11::basic_string<char>*' in initialization
7 | static inline T* getOrCreate( ( []() -> auto) creationSpren *) {
| ^~~~~~~~~~~~
| |
| <lambda()>
<source>:22:31: error: 'getOrCreate<std::__cxx11::basic_string<char> >' cannot be used as a function
22 | auto& zzz = getOrCreate<S>(xx);
| ~~~~~~~~~~~~~~^~~~
Execution build compiler returned: 1
2条答案
按热度按时间yjghlzjz1#
如果你看一下标准库和它们接受可调用对象的函数,它使用 templates。
我建议您也这样做:
您当前的函数还有另一个问题:变量
assigned
是一个普通的局部变量,每次调用getOrCreate
时都会创建并初始化为false
。您需要将其设置为
static
局部变量。如果需要将参数传递给
getOrCreate
,然后将其转发给creationSpren
函数,则使用 * 模板参数包 *:o0lyfsai2#
你现在所拥有的只是一个复杂的静态局部变量。(通常用于单例)
https://godbolt.org/z/GnsGaqnx3