仅在 GCC 4.8.2 x86 (devtoolset-2) 上使用前导包扩展进行模板推导失败
Template deduction with leading pack expansion fails on GCC 4.8.2 x86 (devtoolset-2) only
这是我的 C++11 项目中一些模板元黑客的 MCVE:
#include <functional>
struct Foo {};
template <typename... T>
void expect(std::function<void(const T&)>&&... onSuccess)
{
expect<T...>(
std::forward<std::function<void(const T&)>>(onSuccess)...,
0,
[](){}
);
}
template <typename... T>
void expect(
std::function<void(const T&)>&&... onSuccess,
const time_t timeout,
std::function<void()>&& onExpiry
)
{
// ...
}
int main()
{
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
}
它在 GCC 6.1(using Coliru), GCC 4.8.5 and GCC 4.8.2 x86_64(using GodBolt) 中构建良好,但是当我将它插入我的开发环境(通过 devtoolset-2 的 GCC 4.8.2 CentOS 6 x86) 我收到错误:
[root@localhost ~]# g++ test.cpp -std=c++11 -o test
test.cpp: In function ‘int main()’:
test.cpp:27:47: error: no matching function for call to ‘expect(main()::__lambda1, unsigned int, main()::__lambda2)’
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
^
test.cpp:27:47: note: candidates are:
test.cpp:6:6: note: template<class ... T> void expect(std::function<void(const T&)>&& ...)
void expect(std::function<void(const T&)>&&... onSuccess)
^
test.cpp:6:6: note: template argument deduction/substitution failed:
test.cpp:27:47: note: mismatched types ‘std::function<void(const T&)>’ and ‘unsigned int’
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
^
test.cpp:16:6: note: template<class ... T> void expect(std::function<void(const T&)>&& ..., time_t, std::function<void()>&&)
void expect(
^
test.cpp:16:6: note: template argument deduction/substitution failed:
test.cpp:27:47: note: cannot convert ‘<lambda closure object>main()::__lambda1{}’ (type ‘main()::__lambda1’) to type ‘time_t {aka long int}’
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
[root@localhost ~]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-2/root/usr/libexec/gcc/i686-redhat-linux/4.8.2/lto-wrapper
Target: i686-redhat-linux
Configured with: ../configure --prefix=/opt/rh/devtoolset-2/root/usr --mandir=/opt/rh/devtoolset-2/root/usr/share/man --infodir=/opt/rh/devtoolset-2/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,fortran,lto --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-i686-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-i686-redhat-linux/cloog-install --with-mpc=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-i686-redhat-linux/mpc-install --with-tune=generic --with-arch=i686 --build=i686-redhat-linux
Thread model: posix
gcc version 4.8.2 20140120 (Red Hat 4.8.2-15) (GCC)
我可以通过在第二次重载中将 onSuccess
移动到 timeout
和 onExpiry
之后来解决这个问题,但我认为原始顺序对开发人员来说更容易使用并且更愿意可以的话留着吧
那还有没有别的办法呢?
还有,对于奖励积分,究竟是怎么回事?
仔细查看 GodBolt 界面会发现“4.8.2 x86_64”实际上是 4.8.5(并且“4.8.1 x86_64”重现),所以这看起来是毕竟是一个简单的 GCC 错误(尽管我在 Bugzilla 上看不到它)。
然后我将采用重新排序的解决方法,将可变参数保持在右侧。
我真的不知道 "why" 但是如果你颠倒两个声明就可以了:
#include <functional>
struct Foo {};
template <typename... T>
void expect(
std::function<void(const T&)>&&... onSuccess,
const time_t timeout,
std::function<void()>&& onExpiry
)
{
}
template <typename... T>
void expect(std::function<void(const T&)>&&... onSuccess)
{
expect<T...>(
std::forward<std::function<void(const T&)>>(onSuccess)...,
0,
[](){}
);
}
int main()
{
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
expect<Foo, Foo>([=](const Foo&) {}, [=](const Foo&) {}, 42u, [=](){});
expect<Foo, Foo>([=](const Foo&) {}, [=](const Foo&) {});
}
这是我的 C++11 项目中一些模板元黑客的 MCVE:
#include <functional>
struct Foo {};
template <typename... T>
void expect(std::function<void(const T&)>&&... onSuccess)
{
expect<T...>(
std::forward<std::function<void(const T&)>>(onSuccess)...,
0,
[](){}
);
}
template <typename... T>
void expect(
std::function<void(const T&)>&&... onSuccess,
const time_t timeout,
std::function<void()>&& onExpiry
)
{
// ...
}
int main()
{
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
}
它在 GCC 6.1(using Coliru), GCC 4.8.5 and GCC 4.8.2 x86_64(using GodBolt) 中构建良好,但是当我将它插入我的开发环境(通过 devtoolset-2 的 GCC 4.8.2 CentOS 6 x86) 我收到错误:
[root@localhost ~]# g++ test.cpp -std=c++11 -o test
test.cpp: In function ‘int main()’:
test.cpp:27:47: error: no matching function for call to ‘expect(main()::__lambda1, unsigned int, main()::__lambda2)’
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
^
test.cpp:27:47: note: candidates are:
test.cpp:6:6: note: template<class ... T> void expect(std::function<void(const T&)>&& ...)
void expect(std::function<void(const T&)>&&... onSuccess)
^
test.cpp:6:6: note: template argument deduction/substitution failed:
test.cpp:27:47: note: mismatched types ‘std::function<void(const T&)>’ and ‘unsigned int’
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
^
test.cpp:16:6: note: template<class ... T> void expect(std::function<void(const T&)>&& ..., time_t, std::function<void()>&&)
void expect(
^
test.cpp:16:6: note: template argument deduction/substitution failed:
test.cpp:27:47: note: cannot convert ‘<lambda closure object>main()::__lambda1{}’ (type ‘main()::__lambda1’) to type ‘time_t {aka long int}’
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
[root@localhost ~]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-2/root/usr/libexec/gcc/i686-redhat-linux/4.8.2/lto-wrapper
Target: i686-redhat-linux
Configured with: ../configure --prefix=/opt/rh/devtoolset-2/root/usr --mandir=/opt/rh/devtoolset-2/root/usr/share/man --infodir=/opt/rh/devtoolset-2/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,fortran,lto --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-i686-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-i686-redhat-linux/cloog-install --with-mpc=/builddir/build/BUILD/gcc-4.8.2-20140120/obj-i686-redhat-linux/mpc-install --with-tune=generic --with-arch=i686 --build=i686-redhat-linux
Thread model: posix
gcc version 4.8.2 20140120 (Red Hat 4.8.2-15) (GCC)
我可以通过在第二次重载中将 onSuccess
移动到 timeout
和 onExpiry
之后来解决这个问题,但我认为原始顺序对开发人员来说更容易使用并且更愿意可以的话留着吧
那还有没有别的办法呢?
还有,对于奖励积分,究竟是怎么回事?
仔细查看 GodBolt 界面会发现“4.8.2 x86_64”实际上是 4.8.5(并且“4.8.1 x86_64”重现),所以这看起来是毕竟是一个简单的 GCC 错误(尽管我在 Bugzilla 上看不到它)。
然后我将采用重新排序的解决方法,将可变参数保持在右侧。
我真的不知道 "why" 但是如果你颠倒两个声明就可以了:
#include <functional>
struct Foo {};
template <typename... T>
void expect(
std::function<void(const T&)>&&... onSuccess,
const time_t timeout,
std::function<void()>&& onExpiry
)
{
}
template <typename... T>
void expect(std::function<void(const T&)>&&... onSuccess)
{
expect<T...>(
std::forward<std::function<void(const T&)>>(onSuccess)...,
0,
[](){}
);
}
int main()
{
expect<Foo>([=](const Foo&) {}, 42u, [=](){});
expect<Foo, Foo>([=](const Foo&) {}, [=](const Foo&) {}, 42u, [=](){});
expect<Foo, Foo>([=](const Foo&) {}, [=](const Foo&) {});
}