在尝试编写一个用于讨论元函数类的简单示例时,我写了以下内容:
#include <type_traits>
struct first {
template <typename T, typename U>
using apply = T;
};
template <typename C, typename... Args>
struct curry {
struct type {
template <typename... OtherArgs>
using apply = typename C::template apply<Args..., OtherArgs...>;
};
};
int main() {
static_assert(std::is_same<first::apply<int, char>, int>::value, ""); // OK
using AlwaysInt = curry<first, int>::type;
static_assert(std::is_same<AlwaysInt::apply<char>, int>::value, ""); // error
}
第二个static_assert
无法在gcc 5.1上编译:
main.cpp:17:72: error: pack expansion argument for non-pack parameter 'U' of alias template 'template<class T, class U> using apply = T'
using apply = typename C::template apply<Args..., OtherArgs...>;
^
和clang 3.6:
main.cpp:17:59: error: pack expansion used as argument for non-pack parameter of alias template
using apply = typename C::template apply<Args..., OtherArgs...>;
^~~~~~~~~~~~
两种情况都有相同的错误。但是,如果我将curry
中的应用程序外包给单独的元函数:
template <typename C, typename... Args>
struct eval {
using type = typename C::template apply<Args...>;
};
template <typename C, typename... Args>
struct curry {
struct type {
template <typename... OtherArgs>
using apply = typename eval<C, Args..., OtherArgs...>::type;
};
};
两个编译器都编译just fine。原始示例是否有问题,或者这只是两个编译器中的错误?