下面是兩個大部分相同的模板PgmArray
和PgmArrayF
。第一個適用於左值參考模板參數,第二個適用於整型參數。我喜歡將這兩個合併爲一個:組合兩個幾乎相同的類模板
#include <stdint.h>
#include <type_traits>
#include <array>
#include <iostream>
template<typename T, const T&... Ts>
struct PgmArray final {
static constexpr uint8_t size = sizeof... (Ts);
static constexpr T data[] {Ts...};
};
template<typename T, T... Ts>
struct PgmArrayF final {
static constexpr uint8_t size = sizeof... (Ts);
static constexpr T data[] {Ts...};
};
struct A{
uint8_t m = 0;
};
constexpr A a1{1};
constexpr A a2{2};
constexpr auto x1 = PgmArray<A, a1, a2>{}; // ok
constexpr auto x2 = PgmArrayF<int, 1, 2>{}; // ok
//constexpr auto x3 = PgmArrayF<A, a1, a2>{}; // nok
//constexpr auto x4 = PgmArray<int, 1, 2>{}; // nok
int main() {
}
優秀!不幸的是,這產生ICE與g ++> = 8.0(g ++ 7.1是好的)。 – wimalopaan