COBOL の条件式の不思議な記法はなんだったのか。
あれを他の言語でシミュレーションできないか考えてちょっとやってみた。
久々の C++ なのでちょっとアレな部分が多いけど。
#include <stdio.h> template <typename T> class cComparationResult { private: typedef cComparationResult<T> ThisType; private: bool m_Value; private: T& m_Operand; public: bool getValue() const { return m_Value; } public: const T& getOperand() const { return m_Operand; } public: cComparationResult(bool value, T& operand) : m_Value(value) , m_Operand(operand) { } public: ThisType operator== (T& rhs) const { return ThisType(m_Value && m_Operand == rhs, rhs); } public: ThisType operator!= (T& rhs) const { return ThisType(m_Value && m_Operand != rhs, rhs); } public: operator bool() const { return m_Value; } }; template <typename T> class tcPrimitiveWrapper { private: typedef tcPrimitiveWrapper<T> ThisType; private: T m_Value; public: tcPrimitiveWrapper(T value) : m_Value(value) {} public: cComparationResult<ThisType> operator== (ThisType& rhs) const { printf("==\n"); return cComparationResult<ThisType>(this->m_Value == rhs.m_Value, rhs); } public: cComparationResult<ThisType> operator!= (ThisType& rhs) const { printf("!=\n"); return cComparationResult<ThisType>(this->m_Value != rhs.m_Value, rhs); } }; int main(int argc, char** argv) { typedef tcPrimitiveWrapper<int> cInt; cInt hoge = 1000; cInt mage = 1000; cInt hage = 1000; // COBOL だとこういうのが真になる、 IF HOGE = MAGE = HAGE みたいなのが。 if (hoge == mage == hage) { printf("true\n"); } else { printf("false\n"); } return 0; }
もちろん実用性はないので真似しないように。