cdsfiui 发表于 2016-07-08 12:03

一个C++17 parameter pack的编译错误,如何解决?

如题,我在网上找到一个parameter pack的例子,如下程序:
#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编译 g++ -std=c++17 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
都会有下面的编译错误:
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]
};
上面这些是什么意思呢? 没有看懂啊

neodreamerus 发表于 2016-07-11 02:05

C++17?上面这些是什么意思呢? 没有看懂啊

fender0107401 发表于 2016-07-11 09:14

太可怕了,都有人用C++17了。

neodreamerus 发表于 2016-07-11 10:01

C++11就有 parameter pack吧, C++17的这个功能又增强了?

bandaotidejia 发表于 2016-07-11 17:46

太可怕了,已经出17了,11都看得头疼
页: [1]
查看完整版本: 一个C++17 parameter pack的编译错误,如何解决?