From 2c8542b5149cbd0264fe1e4829097921b4b7d12b Mon Sep 17 00:00:00 2001 From: EternalPhane Date: Mon, 29 Jan 2018 20:48:07 +0800 Subject: [PATCH 1/7] let Any::Derived::clone depend on whether T is copy-constructible --- runtime/Cpp/runtime/src/support/Any.h | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index 3d8845c70..a14fd379a 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -41,14 +41,14 @@ struct ANTLR4CPP_PUBLIC Any } template - Any(U&& value) : _ptr(new Derived>(std::forward(value))) { + Any(U&& value) : _ptr(new Derived, std::is_copy_constructible>::value>(std::forward(value))) { } template bool is() const { typedef StorageType T; - auto derived = dynamic_cast *>(_ptr); + auto derived = dynamic_cast> *>(_ptr); return derived != nullptr; } @@ -57,7 +57,7 @@ struct ANTLR4CPP_PUBLIC Any StorageType& as() { typedef StorageType T; - auto derived = dynamic_cast*>(_ptr); + auto derived = dynamic_cast>*>(_ptr); if (!derived) throw std::bad_cast(); @@ -104,8 +104,11 @@ private: virtual Base* clone() const = 0; }; + template + struct Derived; + template - struct Derived : Base + struct Derived : Base { template Derived(U&& value_) : value(std::forward(value_)) { } @@ -113,7 +116,21 @@ private: T value; Base* clone() const { - return new Derived(value); + return new Derived>(value); + } + + }; + + template + struct Derived : Base + { + template Derived(U&& value_) : value(std::forward(value_)) { + } + + T value; + + Base* clone() const { + return nullptr; } }; From 940e174b5e0825b39bcda4a370f2b658278a0306 Mon Sep 17 00:00:00 2001 From: EternalPhane Date: Mon, 29 Jan 2018 21:38:15 +0800 Subject: [PATCH 2/7] fix typos --- runtime/Cpp/runtime/src/support/Any.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index a14fd379a..d6b4990b8 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -48,7 +48,7 @@ struct ANTLR4CPP_PUBLIC Any bool is() const { typedef StorageType T; - auto derived = dynamic_cast> *>(_ptr); + auto derived = dynamic_cast::value> *>(_ptr); return derived != nullptr; } @@ -57,7 +57,7 @@ struct ANTLR4CPP_PUBLIC Any StorageType& as() { typedef StorageType T; - auto derived = dynamic_cast>*>(_ptr); + auto derived = dynamic_cast::value>*>(_ptr); if (!derived) throw std::bad_cast(); @@ -116,7 +116,7 @@ private: T value; Base* clone() const { - return new Derived>(value); + return new Derived::value>(value); } }; From d027f00aca19962b2de98a428a9a1b8b98707721 Mon Sep 17 00:00:00 2001 From: EternalPhane Date: Tue, 30 Jan 2018 00:17:41 +0800 Subject: [PATCH 3/7] add patch to std::vector --- runtime/Cpp/runtime/src/support/Any.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index d6b4990b8..0e72b5718 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -41,14 +41,14 @@ struct ANTLR4CPP_PUBLIC Any } template - Any(U&& value) : _ptr(new Derived, std::is_copy_constructible>::value>(std::forward(value))) { + Any(U&& value) : _ptr(new Derived, Cloneable>::value>(std::forward(value))) { } template bool is() const { typedef StorageType T; - auto derived = dynamic_cast::value> *>(_ptr); + auto derived = dynamic_cast::value> *>(_ptr); return derived != nullptr; } @@ -57,7 +57,7 @@ struct ANTLR4CPP_PUBLIC Any StorageType& as() { typedef StorageType T; - auto derived = dynamic_cast::value>*>(_ptr); + auto derived = dynamic_cast::value>*>(_ptr); if (!derived) throw std::bad_cast(); @@ -99,6 +99,11 @@ struct ANTLR4CPP_PUBLIC Any } private: + template + struct Cloneable : std::is_copy_constructible {}; + template + struct Cloneable> : std::is_copy_constructible {}; + struct Base { virtual ~Base(); virtual Base* clone() const = 0; @@ -116,7 +121,7 @@ private: T value; Base* clone() const { - return new Derived::value>(value); + return new Derived::value>(value); } }; From 6b8e9824a670fe81601805e6ebce24446ca3aacf Mon Sep 17 00:00:00 2001 From: EternalPhane Date: Sat, 3 Feb 2018 17:12:09 +0800 Subject: [PATCH 4/7] fix #2211 --- contributors.txt | 1 + runtime/Cpp/runtime/src/support/Any.cpp | 3 --- runtime/Cpp/runtime/src/support/Any.h | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/contributors.txt b/contributors.txt index ffd3b9b2e..e2124b25d 100644 --- a/contributors.txt +++ b/contributors.txt @@ -179,3 +179,4 @@ YYYY/MM/DD, github id, Full name, email 2017/12/01, SebastianLng, Sebastian Lang, sebastian.lang@outlook.com 2017/12/03, oranoran, Oran Epelbaum, oran / epelbaum me 2017/12/27, jkmar, Jakub Marciniszyn, marciniszyn.jk@gmail.com +2018/01/29, EternalPhane, Zongyuan Zuo, eternalphane@gmail.com diff --git a/runtime/Cpp/runtime/src/support/Any.cpp b/runtime/Cpp/runtime/src/support/Any.cpp index 3dd1a94bf..b324cc15d 100644 --- a/runtime/Cpp/runtime/src/support/Any.cpp +++ b/runtime/Cpp/runtime/src/support/Any.cpp @@ -11,6 +11,3 @@ Any::~Any() { delete _ptr; } - -Any::Base::~Base() { -} diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index 0e72b5718..aa08ac0cc 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -105,7 +105,7 @@ private: struct Cloneable> : std::is_copy_constructible {}; struct Base { - virtual ~Base(); + virtual ~Base() {}; virtual Base* clone() const = 0; }; From 54297cd329c4e784ae4ce28e0d5598926693571c Mon Sep 17 00:00:00 2001 From: Zongyuan Zuo Date: Mon, 26 Feb 2018 19:51:21 +0800 Subject: [PATCH 5/7] rewrite + add patch to all stl containers --- runtime/Cpp/runtime/src/support/Any.h | 34 +++++++++++---------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index aa08ac0cc..ebd359d1d 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -41,14 +41,14 @@ struct ANTLR4CPP_PUBLIC Any } template - Any(U&& value) : _ptr(new Derived, Cloneable>::value>(std::forward(value))) { + Any(U&& value) : _ptr(new Derived>(std::forward(value))) { } template bool is() const { typedef StorageType T; - auto derived = dynamic_cast::value> *>(_ptr); + auto derived = dynamic_cast *>(_ptr); return derived != nullptr; } @@ -57,7 +57,7 @@ struct ANTLR4CPP_PUBLIC Any StorageType& as() { typedef StorageType T; - auto derived = dynamic_cast::value>*>(_ptr); + auto derived = dynamic_cast*>(_ptr); if (!derived) throw std::bad_cast(); @@ -99,21 +99,18 @@ struct ANTLR4CPP_PUBLIC Any } private: - template + template struct Cloneable : std::is_copy_constructible {}; - template - struct Cloneable> : std::is_copy_constructible {}; + template class C> + struct Cloneable, typename std::enable_if>::value && !std::is_nothrow_copy_constructible>::value>::type> : std::is_copy_constructible {}; struct Base { virtual ~Base() {}; virtual Base* clone() const = 0; }; - template - struct Derived; - template - struct Derived : Base + struct Derived : Base { template Derived(U&& value_) : value(std::forward(value_)) { } @@ -121,20 +118,17 @@ private: T value; Base* clone() const { - return new Derived::value>(value); + return clone<>(); } - }; - - template - struct Derived : Base - { - template Derived(U&& value_) : value(std::forward(value_)) { + private: + template + auto clone() const -> typename std::enable_if::value, Base*>::type { + return new Derived(value); } - T value; - - Base* clone() const { + template + auto clone() const -> typename std::enable_if::value, Base*>::type { return nullptr; } From b4a43a886d17a01acee9e6e8c496bbe7bf2a2a1e Mon Sep 17 00:00:00 2001 From: Zongyuan Zuo Date: Mon, 26 Feb 2018 19:55:51 +0800 Subject: [PATCH 6/7] fix typos --- runtime/Cpp/runtime/src/support/Any.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index ebd359d1d..510f827dc 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -110,7 +110,7 @@ private: }; template - struct Derived : Base + struct Derived : Base { template Derived(U&& value_) : value(std::forward(value_)) { } From 8fb3b42ded91adebd8cf4ce0339b122f1d297c37 Mon Sep 17 00:00:00 2001 From: EternalPhane Date: Mon, 26 Mar 2018 20:31:51 +0800 Subject: [PATCH 7/7] rewrite with std::is_nothrow_copy_constructible --- runtime/Cpp/runtime/src/support/Any.h | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/runtime/Cpp/runtime/src/support/Any.h b/runtime/Cpp/runtime/src/support/Any.h index 510f827dc..817490a3a 100644 --- a/runtime/Cpp/runtime/src/support/Any.h +++ b/runtime/Cpp/runtime/src/support/Any.h @@ -99,11 +99,6 @@ struct ANTLR4CPP_PUBLIC Any } private: - template - struct Cloneable : std::is_copy_constructible {}; - template class C> - struct Cloneable, typename std::enable_if>::value && !std::is_nothrow_copy_constructible>::value>::type> : std::is_copy_constructible {}; - struct Base { virtual ~Base() {}; virtual Base* clone() const = 0; @@ -122,13 +117,13 @@ private: } private: - template - auto clone() const -> typename std::enable_if::value, Base*>::type { + template::value, int>::type = 0> + Base* clone() const { return new Derived(value); } - template - auto clone() const -> typename std::enable_if::value, Base*>::type { + template::value, int>::type = 0> + Base* clone() const { return nullptr; }