【问题标题】:Can't apply int to result of partial_apply无法将 int 应用于 partial_apply 的结果
【发布时间】:2018-07-12 06:43:28
【问题描述】:
// partial_apply.hpp
template <template <class...> class Op, class ...Ts>
struct partial_apply {
    template <class ...Args> using type = Op<Ts..., Args...>;
};
# define PARTIAL_APPLY_T(OP, ...) typename partial_apply<OP, ##__VA_ARGS__>::template type

// test_partial_apply.cc
#include "partial_apply.hpp"

template <class ...Ts> struct A {};
template <template <class...> class Op> using apply_int = Op<int>;

int main() {
    using type = apply_int<PARTIAL_APPLY_T(A)>;
}

我使用命令clang++-5.0 -std=c++17 test_partial_apply.cc 编译了test_partial_apply.cc,它发出了以下错误:

test_partial_apply.cc:8:28: error: expected an identifier or template-id after '::'
    using type = apply_int<PARTIAL_APPLY_T(A)>;
                           ^~~~~~~~~~~~~~~~~~
./../include/partial_apply.hpp:9:78: note: expanded from macro 'PARTIAL_APPLY_T'
# define PARTIAL_APPLY_T(OP, ...) typename partial_apply<OP, ##__VA_ARGS__>::template type
                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
test_partial_apply.cc:8:28: error: expected a type
./../include/partial_apply.hpp:9:78: note: expanded from macro 'PARTIAL_APPLY_T'
# define PARTIAL_APPLY_T(OP, ...) typename partial_apply<OP, ##__VA_ARGS__>::template type
                                                                             ^
2 errors generated.

【问题讨论】:

    标签: c++ templates metaprogramming template-meta-programming


    【解决方案1】:

    只有当它确实是一个类型时才使用typename 消歧器。不指定模板参数template &lt;class ...Args&gt; using type = Op&lt;Ts..., Args...&gt;;不是一个类型。

    删除typename,它应该可以工作:

    # define PARTIAL_APPLY_T(OP, ...) partial_apply<OP, ##__VA_ARGS__>::template type
    

    Live Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多