[LLVMdev] Question about "const"

Stepan Dyatkovskiy STPWORLD at narod.ru
Wed Feb 15 22:36:11 PST 2012


Hi all. I have a next problem. I need to implement some object that will work with both const and "constless" object: Instruction* and const Instruction*, for example.
How to implement it better?
Currently I see two possible ways:

1. Templates.
template <class InstructionTy>
class InstructionProcessorT {
  InstructionTy *Inst;
  InstructionProcessor(InstructionTy *I) : Inst(I) {}
  //
};
typedef InstructionProcessorT<Instruction> InstructionProcessor;
typedef InstructionProcessorT<const Instruction> ConstInstructionProcessor;

2. Unions.
class InstructionProcessor {
  union {
    const Instruction *ConstInst;
    Instruction *Inst;
  }
  InstructionProcessor(Instruction *I) : Inst(I) {}
  InstructionProcessor(const Instruction *I) : ConstInst(I) {}
  
  void someMethod(); // Will work with Inst.
  void someMethod() const; // Will work with ConstInst.
};

Any suggestions?

Thanks!
-Stepan.



More information about the llvm-dev mailing list