我认为这段代码会比英语更好地解释自己:
namespace SC
{
struct GraphicalDatumBase
{
struct SwitcherBase
{
virtual void switchBuffers(){}
};
static std::vector<SwitcherBase*> switchers;
static void switchBuffers();
virtual void update(){}
};
template<class Datum>
struct GraphicalDatum: public GraphicalDatumBase
{
GraphicalDatum(Datum& originalDatum):
originalDatum(originalDatum)
{
for(; doubleBufferIndex < doubleBuffers.size(); ++doubleBufferIndex)
{
if(doubleBuffers[doubleBufferIndex].isVacant)
{
doubleBuffers[doubleBufferIndex].occupy(originalDatum);
return;
}
}
doubleBuffers.emplace_back(originalDatum);
}
Datum& datum(){ return doubleBuffers[doubleBufferIndex].readBuffer(); }
void update()
{
doubleBuffers[doubleBufferIndex].writeBuffer() = originalDatum;
doubleBuffersToBeSwitched.push_back(doubleBufferIndex);
}
private:
struct DoubleBuffer
{
struct
{
uint8_t bufferSwitch: 1;
uint8_t isVacant: 1;
};
Datum buffer0;
Datum buffer1;
DoubleBuffer(const Datum& original):
bufferSwitch(0),
isVacant(0),
buffer0(original),
buffer1(original){}
void occupy(const Datum& original)
{
bufferSwitch = 0;
isVacant = 0;
buffer0 = original;
}
Datum& readBuffer(){ return (bufferSwitch? buffer1 : buffer0); }
Datum& writeBuffer(){ return (bufferSwitch? buffer0 : buffer1); }
void switchBuffer(){ bufferSwitch = !bufferSwitch; }
};
struct Switcher: public SwitcherBase
{
Switcher()
{
switchers.emplace_back(this);
}
void switchBuffers()
{
for(uint32_t d = 0; d < doubleBuffersToBeSwitched.size(); ++d)
{
doubleBuffers[doubleBuffersToBeSwitched[d]].switchBuffer();
}
doubleBuffersToBeSwitched.clear();
}
};
static Switcher switcher;
static std::vector<DoubleBuffer> doubleBuffers;
static std::vector<uint32_t> doubleBuffersToBeSwitched;
void switchBuffers();
Datum& originalDatum;
int32_t doubleBufferIndex = 0;
};
template<class Datum>
std::vector<GraphicalDatum<Datum>::DoubleBuffer> GraphicalDatum<Datum>::doubleBuffers;
...
我尝试定义doubleBuffers
的最后一行产生(在gcc中)这些错误:
type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
expected a type, got ‘SC::GraphicalDatum<Datum>::DoubleBuffer’
template argument 2 is invalid
我的印象是编译器在使用模板作为矢量模板参数时遇到问题。有没有办法解决这个问题?