From d317cc2ea74f7de4e5bccdf1ddb2a9f4f09af2f2 Mon Sep 17 00:00:00 2001 From: LPTK Date: Wed, 12 Jun 2013 15:59:07 +0200 Subject: [PATCH] fixed test & impl --- eo/src/eoOptional.h | 8 ++++++-- eo/test/CMakeLists.txt | 1 + eo/test/t-eoOptional.cpp | 10 ++++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/eo/src/eoOptional.h b/eo/src/eoOptional.h index 5c7bc58c7..9143be3cd 100644 --- a/eo/src/eoOptional.h +++ b/eo/src/eoOptional.h @@ -27,6 +27,7 @@ Lionel Parreaux /** A utility class for wrapping non-const references and use them as default arguments in functions. +This especially is useful for avoiding constructor multiplication. For example, this is not valid C++98 code: @@ -72,7 +73,9 @@ MyClass mc3(t); #ifndef _EOOPTIONAL_H #define _EOOPTIONAL_H +#include #include +//#include template< class T > @@ -98,12 +101,13 @@ public: { if (!hasValue()) throw std::runtime_error("Cannot get a reference from a eoOptional wrapper with no value"); + //throw eoEx; return *_val; } - T& getOr (T& default) const + T& getOr (T& defaultValue) const { - return hasValue()? *_val: default; + return hasValue()? *_val: defaultValue; } protected: diff --git a/eo/test/CMakeLists.txt b/eo/test/CMakeLists.txt index 15fd22464..e283ab958 100644 --- a/eo/test/CMakeLists.txt +++ b/eo/test/CMakeLists.txt @@ -69,6 +69,7 @@ set (TEST_LIST #t-openmp # does not work anymore since functions used in this test were removed from EO #t-eoDualFitness t-eoParser + t-eoOptional ) diff --git a/eo/test/t-eoOptional.cpp b/eo/test/t-eoOptional.cpp index e22781a24..bee58957c 100644 --- a/eo/test/t-eoOptional.cpp +++ b/eo/test/t-eoOptional.cpp @@ -6,12 +6,14 @@ //----------------------------------------------------------------------------- -typedef T int; +typedef int T; struct MyClass { MyClass(eoOptional my_T = NULL) - : actual_T(my_T.getOr(default_T)) - { } + : default_T(42), actual_T(my_T.getOr(default_T)) + { + std::cout << "Value " << actual_T << " was used for construction" << std::endl; + } private: T default_T; T& actual_T; @@ -22,6 +24,6 @@ int main(int ac, char** av) // Three ways of using MyClass: MyClass mc1; MyClass mc2(NULL); - T t; + T t(666); MyClass mc3(t); }