![](/img/trans.png)
[英]g++ and clang++ different behaviour with pointer to variadic template functions
[英]Variadic template using lambdas : error with g++ but running with clang++
在使用可變參數模板,類,函數和lambdas時( 從這里 )我發現以下代碼在運行時使用clang++
而不是使用g++
運行:
#include <iostream>
#include <string>
using namespace std;
template <class... F>
struct overload_set : F...
{
overload_set(F... f) : F(f)... {}
};
template <class... F>
auto overload(F... f)
{
return overload_set<F...>(f...);
}
int main()
{
auto func = overload (
[](int &val) { val *= 2; },
[](string &arg) { arg += arg; },
[](char &c) { c = 'x'; }
);
int val = 10;
string str = "stackoverflow";
char ch = 's';
cout << val << " : " << str << " : " << ch << endl;
func(val);
func(str);
func(ch);
cout << val << " : " << str << " : " << ch << endl;
return 0;
}
對於clang
: coliru
對於g++
: coliru
g++
為func(val)
, func(str)
和func(c)
賦予了模糊的operator()
func(c)
。 我認為operator()
不能含糊不清,因為每個都有不同的參數。
g++
什么問題?
這與lambdas,可變參數模板,運算符或任何高級C ++ 1 {xy}東西幾乎沒有關系。 讓我們簡化一下:
struct foo
{
void func(int&){}
};
struct bar
{
void func(char&){}
};
struct test : foo, bar {};
int main()
{
test t;
int i = 1;
char c = 'a';
t.func(i);
t.func(c);
}
這無法在g++
或clang++
。 這也是一件好事,因為這是指定語言的工作方式。
如果我們將func
更改為operator()
, g++
繼續拒絕該程序,但clang++
接受或拒絕它,具體取決於調用運算符的方式:
t.operator()(c); // rejected
t(c); // accepted
這對我來說就像一個鏗鏘的蟲子。
為了使上面的代碼編譯,需要進行非常小的更改:
struct test : foo, bar {
using foo::func;
using bar::func;
};
現在我不知道如何在using指令中進行包擴展工作,或者它確實可行。 但有一個解決方法:
template <class... F> struct overload_set;
template <> struct overload_set<> {};
template <class F> struct overload_set<F> : F {
using F::operator();
overload_set(F f) : F(f) {}
};
template <class F, class... Fs>
struct overload_set<F, Fs...> : F, overload_set<Fs...>
{
overload_set(F f, Fs... fs) : F(f), overload_set<Fs...>(fs...) {}
using F::operator();
using overload_set<Fs...>::operator();
};
通過此更改,您的代碼將使用g++
和clang++
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.