可变参数模板中的最大尺寸元函数

时间:2017-09-05 16:13:49

标签: c++ c++11 templates metaprogramming variadic-templates

我试图在可变参数模板中实现元函数(?),以便在编译时计算几种类型的sizeof的最大值。

template<typename... Ts> struct MaxSizeof {
  static constexpr size_t value = 0;
};

template<typename T, typename... Ts> struct MaxSizeof {
  static constexpr size_t value = std::max(sizeof(T), typename MaxSizeof<Ts...>::value);
};

但我收到了一些奇怪的错误:

MaxSizeof.h(7): error C3855: 'MaxSizeof': template parameter 'Ts' is incompatible with the declaration
MaxSizeof.h(7): error C2977: 'MaxSizeof': too many template arguments
MaxSizeof.h(5): note: see declaration of 'MaxSizeof'

你能帮忙修理我的代码吗?

编译器是MSVC ++ 2017工具集v141。

4 个答案:

答案 0 :(得分:3)

您的专业化语法不正确,应该是:

      //sleep time so UX can load
        Thread.Sleep(3000);

      //open the new  form
        By newButton = By.XPath("/dummy/xpath");
        IWebElement button = driver.FindElement(newButton);
        button.Click();

答案 1 :(得分:0)

std::max仅在C ++ 14之后标记为constexpr,因此您必须自己编写。此外,您不能重载结构,这是您的代码失败的一个原因。

这是一个需要C ++ 14 std::max的解决方案,您可以根据需要更改为使用自定义的解决方案。

template<typename... Ts>
struct MaxSizeof : std::integral_constant<std::size_t, std::max({sizeof(Ts)...})> {};

答案 2 :(得分:0)

需要修复2个:

  1. 正如@ Phil1970指出的那样,我忘了func nonUniqueElements(array1: [Int], array2: [Int])->[Int] { let countedSet = NSCountedSet(array: array1+array2) return countedSet.flatMap({ element in if countedSet.count(for: element) > 1 { return element as? Int } else { return nil } }) } nonUniqueElements(array1: arrayOne, array2: arrayTwo) static的定义。
  2. 我必须在第7行指定模板参数:value而不是简单地struct MaxSizeof<T, Ts...> {
  3. 所以下面的代码编译:

    struct MaxSizeof {

答案 3 :(得分:0)

需要另一个小修复:

template<typename T, typename... Ts> struct MaxSizeof<T, Ts...> {
  static constexpr size_t value = std::max(sizeof(T), MaxSizeof<Ts...>::value); // there should be with no `typename` 
};