0

The code is meant to solve this problem: If we know a signature and bind params for the first n params, get the new call signature if first n params are bounded.

But gcc gives error when I try this compile the code.

a.cc:23:62: error: expected ‘;’ before ‘<’ token
     using signature = typename GetSignature<R(Args...)>::Bind<BArgs...>::signature;

I have no ideal why there is such error.

Here is the code:

#include <type_traits>

template<typename T>
struct GetSignature {
  using signature = typename T::signature;
};

template<typename R>
struct GetSignature<R(void)> {
  template<typename... Args>
  struct Bind {
    using signature = R(void);
  };  
};

template<typename R, typename Arg, typename... Args>
struct GetSignature<R(Arg, Args...)> {
  template<typename... BArgs> struct Bind;

  template<typename BArg, typename... BArgs>
  struct Bind<BArg, BArgs...> {
    static_assert(std::is_same<Arg, BArg>::value);
    using signature = typename GetSignature<R(Args...)>::Bind<BArgs...>::signature;
  };  

  template<typename BArg>
  struct Bind<BArg> {
    using signature = R(Args...);
  };  
};

int func(bool, double, int, char);

int main() {
  // GetSignature<func>::Bind<bool, double>::signature should be int(int, char)
  return 0;
}
4

1 回答 1

1

我通过简单地替换这一行来编译你的代码

using signature = typename GetSignature<R(Args...)>::Bind<BArgs...>::signature;

有了这个

using signature = typename GetSignature<R(Args...)>::template Bind<BArgs...>::signature;

您应该在Bind之前使用template关键字将其视为依赖模板名称。

之后我可以像这样使用你的模板类:

GetSignature<decltype (func)>::Bind<bool, double>::signature

我的机器上有 gcc 版本 10.2.0。

于 2020-12-22T08:05:06.463 回答