1
#include <iostream>
#include <utility>
using namespace std;
template<typename>
struct Class1;
template<size_t...Is>
struct Class1<index_sequence<Is...>> {
template<typename T, size_t N>
struct Holder {
constexpr Holder(T const(&Ns)[N]) : data{T(Ns[Is] * Is)...} {
}
T data[N];
};
};
template<typename T, T...Ns>
class Class2 {
public:
static constexpr const size_t N = sizeof...(Ns);
static constexpr const T mNs[] = {Ns...};
static constexpr const typename Class1<make_index_sequence<N>>::template Holder<T, N> Hs{mNs};
// If I replace the above N with sizeof...(Ns), the error is gone.
};
template <typename T, T...Ns>
constexpr const typename Class1<make_index_sequence<sizeof...(Ns)>>::template Holder<T, sizeof...(Ns)>
Class2<T, Ns...>::Hs; // g++ error: conflicting declaration.
int main() {
cout << &Class2<int, 1, 2, 3>::Hs << endl;
}
これは私のコードの簡略化されたバージョンです。それはVSがこのくそに合致しないのでVSでコンパイルします。しかし、g ++ 6.3でコンパイルに失敗しました。 Class2のタイプは何ですか?012 ::: :: Hs?クラス静的メンバーの宣言が矛盾します
基本的に、別の配列の要素とコンパイル時にその要素のインデックスを使用して配列を初期化する必要があります。だから、もっと良い方法があれば、それは良いでしょう。
同じエラーを再現するシンプルなコード: '#includeテンプレート struct foo {};テンプレート構造体{static constexpr std :: size_t Dim {sizeof ...(Is)}; static foo f {}; };テンプレート foo bar :: f; int main(){} ' –
max66