【发布时间】:2019-07-31 15:42:39
【问题描述】:
我想要一个辅助函数来为我实例化一个类。目前它无法在 clang 中编译(尽管它在 gcc 中编译工作),但我也需要它在 clang 中工作。目前我正在使用clang version 6.0.0-1ubuntu2。
我不确定它为什么会失败,因为 gcc 能够检测到类型。我尝试从this post 做一些事情并玩了一段时间,但我一直撞到墙上。 MCVE 可用,或者您可以在coliru here 上试用:
#include <vector>
using namespace std;
template <typename T, template <typename> typename Container>
struct SomeClass {
SomeClass(const Container<T>& c) {
}
};
template <typename T, template <typename> typename C>
inline auto make_some_class(const C<T>& container) {
return SomeClass<T, C>(container);
}
int main() {
vector<int> ints;
auto stuff = make_some_class(ints);
}
main.cpp:19:18: 错误:没有匹配的函数调用“make_some_class”
auto stuff = make_some_class(ints); ^~~~~~~~~~~~~~~main.cpp:12:13: 注意:候选模板被忽略:替换失败 [with T = int]:模板模板参数的模板参数与其对应的模板模板参数不同
inline auto make_some_class(const C<T>& container) { ^生成 1 个错误。
【问题讨论】:
-
这看起来像是不可推断的上下文,即使我们暂时忽略
std::vector有两个模板参数,而不是一个,并且一开始就不会匹配template<typename> typename Container的基本事实。跨度> -
我希望您使用的是 Clang,如果您添加编译标志
-frelaxed-template-template-args,它将起作用。 -
@SamVarshavchik 这不是不可演绎的上下文。
标签: c++ templates c++17 variadic-templates template-argument-deduction