C ++ 17参数包编译错误

时间:2016-07-08 04:07:39

标签: c++ gcc c++17 fold-expression

我有一个来自互联网的程序,如下所示,以展示C ++ 17中“参数包”的能力

#include <iostream>
#include <vector>
#include <climits>
#include <cstdint>
#include <type_traits>
#include <utility>

template<typename ...Args>
void printer(Args&&... args) {
    (std::cout << ... << args) << '\n';
}

template<typename T, typename... Args>
void push_back_vec(std::vector<T>& v, Args&&... args)
{
    (v.push_back(args), ...);
}

// compile-time endianness swap based on http://stackoverflow.com/a/36937049 
template<class T, std::size_t... N>
constexpr T bswap_impl(T i, std::index_sequence<N...>) {
  return (((i >> N*CHAR_BIT & std::uint8_t(-1)) << (sizeof(T)-1-N)*CHAR_BIT) | ...);
};
template<class T, class U = std::make_unsigned_t<T>>
constexpr U bswap(T i) {
  return bswap_impl<U>(i, std::make_index_sequence<sizeof(T)>{});
}

int main()
{
    printer(1, 2, 3, "abc");

    std::vector<int> v;
    push_back_vec(v, 6, 2, 45, 12);
    push_back_vec(v, 1, 2, 9);
    for (int i : v) std::cout << i << ' ';

    static_assert(bswap<std::uint16_t>(0x1234u)==0x3412u);
    static_assert(bswap<std::uint64_t>(0x0123456789abcdefULL)==0xefcdab8967452301ULL);
}

但是当我用g ++ 5.3和6.1用-std = c ++ 17编译它时,报告了一个错误,我没有得到错误消息的内容:

main.cpp: In function 'constexpr T bswap_impl(T, std::index_sequence<N ...>)':
main.cpp:22:49: error: binary expression in operand of fold-expression
   return (((i >> N*CHAR_BIT & std::uint8_t(-1)) << (sizeof(T)-1-N)*CHAR_BIT) | ...);
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: At global scope:
main.cpp:23:2: warning: extra ';' [-Wpedantic]
 };

请帮助解释一下吗?非常感谢。

0 个答案:

没有答案