[英]Generic lambda, inheritance and trailing return type: is this valid code?
注意:我正在向clang打開一個問題,但我想確保我的代碼也是有效的。
我試圖回答另一個答案 ,我在玩lambdas和繼承時發現了一些困難。
考慮以下最小的例子:
template<typename Func>
struct Base: Func {
Base(Func func): Func{func} {}
template<typename... Args>
auto operator()(Args... args)
-> decltype(Func::operator()(args...), void()) {
Func::operator()(args...);
}
};
int main() {
auto l = [](auto &&) {};
Base<decltype(l)> mixin{l};
mixin(0);
}
GCC 6.1 編譯它 ,叮叮4.0 崩潰 。
請注意,使用以下定義編譯都很好:
auto l = [](int) {};
這是有效的代碼還是我正在做標准不允許的事情?
這是我剛剛打開的問題的鏈接。
如果您需要clang的解決方案 - 以下代碼應該與clang一起使用
#include <utility>
#include <iostream>
template <typename F>
struct Base : F
{
Base (F f) : F {f} {}
template <typename... Args>
decltype(auto) operator () (Args&&... args)
{
std::cout << "(";
F::operator () (std::forward<Args> (args)...);
std::cout << ")" << std::endl;
}
};
int
main ()
{
auto l = [] (auto && i) {
std::cout << i;
};
Base<decltype(l)> mixin {l};
mixin (0);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.