diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/tag.h | 23 | ||||
| -rw-r--r-- | include/value.h | 8 |
2 files changed, 26 insertions, 5 deletions
diff --git a/include/tag.h b/include/tag.h index 58b99c082c..569da446d6 100644 --- a/include/tag.h +++ b/include/tag.h @@ -71,6 +71,15 @@ public: std::unique_ptr<tag> clone() &&; /** + * @brief Returns a reference to the tag as an instance of T + * @throw std::bad_cast if the tag is not of type T + */ + template<class T> + T& as(); + template<class T> + const T& as() const; + + /** * @brief Move-assigns the given tag if the class is the same * @throw std::bad_cast if @c rhs is not the same type as @c *this */ @@ -101,6 +110,20 @@ private: std::ostream& operator<<(std::ostream& os, tag_type tt); +template<class T> +T& tag::as() +{ + static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag"); + return dynamic_cast<T&>(*this); +} + +template<class T> +const T& tag::as() const +{ + static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag"); + return dynamic_cast<const T&>(*this); +} + } #endif // TAG_H_INCLUDED diff --git a/include/value.h b/include/value.h index c017bcbd27..4865c8fd1c 100644 --- a/include/value.h +++ b/include/value.h @@ -93,7 +93,7 @@ public: const tag& get() const; /** - * @brief Returns the contained tag as an instance of T + * @brief Returns a reference to the contained tag as an instance of T * @throw std::bad_cast if the tag is not of type T */ template<class T> @@ -207,15 +207,13 @@ private: template<class T> T& value::as() { - static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag"); - return dynamic_cast<T&>(get()); + return tag_->as<T>(); } template<class T> const T& value::as() const { - static_assert(std::is_base_of<tag, T>::value, "T must be a subclass of tag"); - return dynamic_cast<T&>(get()); + return tag_->as<T>();; } } |
