- 论坛徽章:
- 2
|
如题,我在网上找到一个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]
- };
复制代码 上面这些是什么意思呢? 没有看懂啊
|
|