我正在嘗試學習模板上的可變引數。我自己強加的練習是使用靜態成員制作 NN。這個想法是在沒有 oa 堆的微控制器上運行它。為此,我想使用模板來定義層之間的笛卡爾積。
即
weights<T,2,3,4>::type
會翻譯成
tuple<array<T,6>, array<T,12>>
#include<iostream>
#include <array>
template<typename T, int left, typename... U>
struct weights {
typedef std::tuple<U...> type;
};
template<typename T, int left, int right, int... other, typename... U>
struct weights {
typedef weights<T, right, other..., std::array<T, left*right>, U...>::type type;
};
int main() {
weights<int, 2, 3, 4>::type o;
return 0;
}
然而,編譯器似乎看到第一個模板引數而不是第二個。
這是我收到的訊息:
weights.cpp:10:8: error: redeclared with 5 template parameters
10 | struct weights {
| ^~~~~~~
weights.cpp:5:8: note: previous declaration ‘template<class T, int left, class ... U> struct weights’ used 3 template parameters
5 | struct weights {
| ^~~~~~~
weights.cpp: In function ‘int main()’:
weights.cpp:15:25: error: type/value mismatch at argument 3 in template parameter list for ‘template<class T, int left, class ... U> struct weights’
15 | weights<int, 2, 3, 4>::type o;
| ^
weights.cpp:15:25: note: expected a type, got ‘3’
weights.cpp:15:25: error: type/value mismatch at argument 3 in template parameter list for ‘template<class T, int left, class ... U> struct weights’
weights.cpp:15:25: note: expected a type, got ‘4’
weights.cpp:15:33: error: expected initializer before ‘o’
15 | weights<int, 2, 3, 4>::type o;
| ^
如何讓編譯器看到不同的簽名?
uj5u.com熱心網友回復:
不確定是否是最好的方法,但這就是我解決它的方法:
#include <iostream>
#include <array>
#include <tuple>
namespace weights_ns {
template <bool last>
struct weights;
template <>
struct weights<true> {
template<typename T, typename... U>
struct supertype_0 {
template<int left> //, int right, int... other>
struct supertype_1 :
public std::tuple<U...>
{};
};
};
template <>
struct weights<false> {
template<typename T, typename... U>
struct supertype_0 {
template<int left, int right, int... other>
struct supertype_1 :
public weights_ns::weights<(sizeof...(other)==0)>::template supertype_0<T, std::array<T, left*right>, U...>::template supertype_1<right, other...>
{};
};
};
};
int main() {
std::cout << "size: " << sizeof(weights_ns::template weights<false>::template supertype_0<char>::template supertype_1<2,3,5>) << std::endl;
}
uj5u.com熱心網友回復:
我不確定如何改進您的答案,但您可以稍微清理一下使用界面
#include <iostream>
#include <array>
#include <tuple>
namespace weights_ns {
template <bool last>
struct container;
template <>
struct container<true> {
template<typename T, typename... U>
struct supertype_0 {
template<int left>
struct supertype_1 :
public std::tuple<U...>
{};
};
};
template <>
struct container<false> {
template<typename T, typename... U>
struct supertype_0 {
template<int left, int right, int... other>
struct supertype_1 :
public weights_ns::container<(sizeof...(other)==0)>::template supertype_0<T, std::array<T, left*right>, U...>::template supertype_1<right, other...>
{};
};
};
template<typename T, T... Layers>
struct weights: public weights_ns::container<(sizeof...(Layers) < 2)>::template supertype_0<T>::template supertype_1<Layers...>
{};
};
int main() {
weights_ns::weights<int, 2, 3, 5> t;
std::cout << "size: " << sizeof(t) << std::endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/385153.html
上一篇:類方法的內容由模板值決定
