根據給出的內容,是否可以讓類模板接受一個 ( unsigned int, typename) 引數中的任何一個?
我的意思的例子:
template<??>
class Bytes
{
// ....
};
Bytes<4> FourBytes;
Bytes<int> FourBytes;
Bytes<DWORD64> EightBytes;
我知道template<auto T>,雖然在想是否有不同的解決方案?
uj5u.com熱心網友回復:
模板引數必須是型別或值,沒有可以是型別或值的占位符。也就是說,您可以創建幾個工廠函式來幫助您。那可能看起來像
template<std::size_t N>
class Bytes
{
// ....
};
template <typename T>
auto make_bytes() { return Bytes<sizeof(T)>{}; }
template <std::size_t N>
auto make_bytes() { return Bytes<N>{}; }
你會宣告像這樣的物件
auto FourBytesValue = make_bytes<4>();
auto FourBytesType = make_bytes<int>();
auto EightBytes = make_bytes<DWORD64>();
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/403232.html
標籤:
