【发布时间】:2021-10-26 12:06:18
【问题描述】:
下面的一小段代码编译并运行
#include <iostream>
using namespace std;
template<class T>
class A {
public:
template<class T2>
void f(T2 a);
};
template<class T>
template<typename T2>
void A<T>::f(T2 a){a();}
int main()
{
A<int> ac;
ac.f([](){cout<<"bla"<<endl;});
}
但是当分割成文件时 hpp:
template<class T>
class A {
public:
template<class T2>
void f(T2 a);
};
cpp:
#include "a.hpp"
#include <iostream>
using namespace std;
template<class T>
template<typename T2>
void A<T>::f(T2 a){a();}
和主要的:
#include "a.hpp"
#include <iostream>
using namespace std;
int main()
{
A<int> ac;
ac.f([](){cout<<"bla"<<endl;});
}
我得到一个编译时错误:
In file included from main.cpp:1:
a.hpp:5:10: error: ‘void A<T>::f(T2) [with T2 = main()::<lambda()>; T = int]’, declared using local type ‘main()::<lambda()>’, is used but never defined [-fpermissive]
5 | void f(T2 a);
| ^
a.hpp:5:10: warning: ‘void A<T>::f(T2) [with T2 = main()::<lambda()>; T = int]’ used but never defined
/usr/bin/ld: cannot find main.o: No such file or directory
collect2: error: ld returned 1 exit status
我假设我需要在cpp文件中实例化模板,例如。
template void A<int>::f<>();
但到目前为止,我还未能正确使用语法。由于 main 中“f”的函数参数作为 lambda,我想知道这是否可能。
非常感谢任何建议。
【问题讨论】:
-
有两个模板,类和函数。我假设您需要显式地实例化两者才能使其正常工作,但由于该函数是使用来自另一个 TU 的 lambda 调用的,因此您无法显式地实例化它。通常,您将实现保留在模板的标题中。
-
嗨。谢谢。但是,将所有内容保留在标题中可以规避问题,但不能解决问题。
-
将所有内容保留在标题中是惯用的方法。但是,如果您绝对不希望这样,则必须放弃使用 lambda,因为它们的类型对于每个 lambda 都是唯一的,并且由编译器生成。 AFAIK 无法明确指定 lambda 的类型。
标签: c++ templates lambda template-meta-programming