【发布时间】:2016-10-07 12:02:38
【问题描述】:
P0091R3 ("Template argument deduction for class templates") 最近被添加到gcc trunk,可以在wandbox 上进行测试。
我想到的是可以用它来实现一个 “作用域守卫”只需几行代码:
scope_guard _([]{ cout << "hi!\n" });
我试过implementing it on wandbox...
template <typename TF>
struct scope_guard : TF
{
scope_guard(TF f) : TF{f} { }
~scope_guard() { (*this)(); }
};
int main()
{
scope_guard _{[]{}};
}
...但是编译失败并出现以下错误:
prog.cc:6:5: error: 'scope_guard(TF)-> scope_guard<TF> [with TF = main()::<lambda()>]', declared using local type 'main()::<lambda()>', is used but never defined [-fpermissive]
scope_guard(TF f) : TF{std::move(f)} { }
^~~~~~~~~~~
然后我尝试了using a non-lambda local type,得到了同样的错误。
int main()
{
struct K { void operator()() {} };
scope_guard _{K{}};
}
然后,I tried a non-local type,它按预期工作。
struct K { void operator()() {} };
int main()
{
scope_guard _{K{}};
}
此功能是否以防止推断本地类型的方式设计?
或者这是gcc当前实现该功能的缺陷吗?
【问题讨论】:
-
看起来您已经找到并评论了错误报告。
标签: c++ templates gcc c++17 template-argument-deduction