解决我的问题;他们都对此给出了相似的答案,但我认为 Barry's 更具表现力,因此我必须将他的回答归功于他。
现在对于 Sombrero,他确实帮助我处理了 using Reg# = Register<T>,所以我不必通过传入它们的类型参数来调用这些模板。
它基本上归结为:
static constexpr auto Bits = sizeof(Type) * CHAR_BIT;
我要感谢那些为我提供帮助的人。
使用来自用户的反馈:Barry 和 SombreroChicken 我能够做到这一点:
main.cpp
#include <iostream>
#include "Register.h"
int main() {
using namespace vpc;
u8 valA = 8;
u16 valB = 16;
u32 valC = 32;
u64 valD = 64;
Reg8 r8A(valA);
Reg8 r8B(valB);
Reg8 r8C(valC);
Reg8 r8D(valD);
Reg16 r16A(valA);
Reg16 r16B(valB);
Reg16 r16C(valC);
Reg16 r16D(valD);
Reg32 r32A(valA);
Reg32 r32B(valB);
Reg32 r32C(valC);
Reg32 r32D(valD);
Reg64 r64A(valA);
Reg64 r64B(valB);
Reg64 r64C(valC);
Reg64 r64D(valD);
std::cout << r8A << r8B << r8C << r8D;
std::cout << r16A << r16B << r16C << r16D;
std::cout << r32A << r32B << r32C << r32D;
std::cout << r64A << r64B << r64C << r64D;
return EXIT_SUCCESS;
}
输出
Reg8(8)
hex: 0x08
bin: 00001000
Reg8(16)
hex: 0x10
bin: 00010000
Reg8(32)
hex: 0x20
bin: 00100000
Reg8(64)
hex: 0x40
bin: 01000000
Reg16(8)
hex: 0x0008
bin: 0000000000001000
Reg16(16)
hex: 0x0010
bin: 0000000000010000
Reg16(32)
hex: 0x0020
bin: 0000000000100000
Reg16(64)
hex: 0x0040
bin: 0000000001000000
Reg32(8)
hex: 0x00000008
bin: 00000000000000000000000000001000
Reg32(16)
hex: 0x00000010
bin: 00000000000000000000000000010000
Reg32(32)
hex: 0x00000020
bin: 00000000000000000000000000100000
Reg32(64)
hex: 0x00000040
bin: 00000000000000000000000001000000
Reg64(8)
hex: 0x0000000000000008
bin: 0000000000000000000000000000000000000000000000000000000000001000
Reg64(16)
hex: 0x0000000000000010
bin: 0000000000000000000000000000000000000000000000000000000000010000
Reg64(32)
hex: 0x0000000000000020
bin: 0000000000000000000000000000000000000000000000000000000000100000
Reg64(64)
hex: 0x0000000000000040
bin: 0000000000000000000000000000000000000000000000000000000001000000
Register.h
#pragma once
#include <algorithm>
#include <bitset>
#include <string>
#include <vector> // include for typedefs below.
namespace vpc {
typedef std::int8_t i8;
typedef std::int16_t i16;
typedef std::int32_t i32;
typedef std::int64_t i64;
typedef std::uint8_t u8;
typedef std::uint16_t u16;
typedef std::uint32_t u32;
typedef std::uint64_t u64;
const u16 BYTE = 0x08;
const u16 WORD = 0x10;
const u16 DWORD = 0x20;
const u16 QWORD = 0x40;
typedef std::bitset<BYTE> Byte;
typedef std::bitset<WORD> Word;
typedef std::bitset<DWORD> DWord;
typedef std::bitset<QWORD> QWord;
template<typename Ty>
struct Register_t {
static constexpr u16 BitCount = sizeof(Ty) * CHAR_BIT;
Ty currentValue;
Ty previousValue;
std::bitset<BitCount> bits;
Register_t() : currentValue{ 0 }, previousValue{ 0 }, bits{ 0 }{}
template<typename U>
explicit Register_t(U val) : currentValue{ static_cast<Ty>(val) }, previousValue{ 0 }, bits{ currentValue } {}
};
template<typename Ty>
struct Register : public Register_t<Ty> {
Register() = default;
explicit Register(Ty val) : Register_t<Ty>( val ) {}
};
using Reg8 = Register<u8>;
using Reg16 = Register<u16>;
using Reg32 = Register<u32>;
using Reg64 = Register<u64>;
std::ostream& operator<<(std::ostream& os, const Reg8& reg);
std::ostream& operator<<(std::ostream& os, const Reg16& reg);
std::ostream& operator<<(std::ostream& os, const Reg32& reg);
std::ostream& operator<<(std::ostream& os, const Reg64& reg);
} // namespace vpc
Register.cpp
#include "Register.h"
#include <iostream>
#include <iomanip>
namespace vpc {
std::ostream& operator<<(std::ostream& os, const Reg8& r) {
os << "Reg8(" << +r.currentValue << ")\n"
<< "hex: " << "0x" << std::uppercase
<< std::setfill('0') << std::setw(2) << std::hex
<< +r.currentValue << std::dec << '\n'
<< "bin: " << r.bits << '\n' << std::endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const Reg16& r) {
os << "Reg16(" << r.currentValue << ")\n"
<< "hex: " << "0x" << std::uppercase
<< std::setfill('0') << std::setw(4) << std::hex
<< r.currentValue << std::dec << '\n'
<< "bin: " << r.bits << '\n' << std::endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const Reg32& r) {
os << "Reg32(" << r.currentValue << ")\n"
<< "hex: " << "0x" << std::uppercase
<< std::setfill('0') << std::setw(8) << std::hex
<< r.currentValue << std::dec << '\n'
<< "bin: " << r.bits << '\n' << std::endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const Reg64& r) {
os << "Reg64(" << r.currentValue << ")\n"
<< "hex: " << "0x" << std::uppercase
<< std::setfill('0') << std::setw(16) << std::hex
<< r.currentValue << std::dec << '\n'
<< "bin: " << r.bits << '\n' << std::endl;
return os;
}
} // namespace vpc
这就是我要找的。p>