From a29894ee2b47cda32efe31cfc5a4b2bca0b283fd Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Mon, 20 May 2019 09:09:12 -0400 Subject: [PATCH] getField() fail to compile when T isn't Field or a sub-class --- src/pv/pvIntrospect.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/pv/pvIntrospect.h b/src/pv/pvIntrospect.h index 1a0ca1c..7a13f0c 100644 --- a/src/pv/pvIntrospect.h +++ b/src/pv/pvIntrospect.h @@ -14,6 +14,8 @@ #include #include +#include + #include #include #include @@ -361,6 +363,8 @@ public: //! @version Added after 7.0.0 std::tr1::shared_ptr build() const; + enum {isField=1}; + protected: /** * Constructor @@ -724,6 +728,7 @@ public: template std::tr1::shared_ptr getField(std::string const &fieldName) const { + STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class FieldConstPtr field(getField(fieldName)); if (field) return std::tr1::dynamic_pointer_cast(field); @@ -742,6 +747,7 @@ public: template std::tr1::shared_ptr getFieldT(std::string const &fieldName) const { + STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class return std::tr1::dynamic_pointer_cast(getFieldT(fieldName)); } @@ -756,6 +762,7 @@ public: template std::tr1::shared_ptr getField(std::size_t index) const { + STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class const FieldConstPtr& field(getField(index)); if (field) return std::tr1::dynamic_pointer_cast(field); @@ -774,6 +781,7 @@ public: template std::tr1::shared_ptr getFieldT(std::size_t index) const { + STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class return std::tr1::dynamic_pointer_cast(getFieldT(index)); } @@ -877,6 +885,7 @@ public: template std::tr1::shared_ptr getField(std::string const &fieldName) const { + STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class FieldConstPtr field = getField(fieldName); if (field) return std::tr1::dynamic_pointer_cast(field); @@ -895,6 +904,7 @@ public: template std::tr1::shared_ptr getFieldT(std::string const &fieldName) const { + STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class return std::tr1::dynamic_pointer_cast(getFieldT(fieldName)); } @@ -909,6 +919,7 @@ public: template std::tr1::shared_ptr getField(std::size_t index) const { + STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class FieldConstPtr field = getField(index); if (field) return std::tr1::dynamic_pointer_cast(field); @@ -927,6 +938,7 @@ public: template std::tr1::shared_ptr getFieldT(std::size_t index) const { + STATIC_ASSERT(FT::isField); // only allow cast from Field sub-class return std::tr1::dynamic_pointer_cast(getFieldT(index)); }