{"action":"created","comment":{"url":"https://api.github.com/repos/mapbox/variant/pulls/comments/111481499","pull_request_review_id":32732958,"id":111481499,"diff_hunk":"@@ -0,0 +1,86 @@\n+#ifndef VARIANT_CAST_HPP\n+#define VARIANT_CAST_HPP\n+\n+#include <type_traits>\n+\n+namespace mapbox { namespace util {\n+\n+namespace detail\n+{\n+\n+template <class T>\n+class static_caster\n+{\n+public:\n+    template <class V>\n+    T &operator()(V &v) const\n+    {\n+        return static_cast<T &>(v);\n+    }\n+};\n+\n+template <class T>\n+class dynamic_caster\n+{\n+public:\n+    using result_type = T&;\n+    template <class V>\n+    T &operator()(V &v, typename std::enable_if<!std::is_polymorphic<V>::value>::type* = nullptr) const\n+    {\n+        throw std::bad_cast();\n+    }\n+    template <class V>\n+    T &operator()(V &v, typename std::enable_if<std::is_polymorphic<V>::value>::type* = nullptr) const\n+    {\n+        return dynamic_cast<T &>(v);\n+    }\n+};\n+\n+template <class T>\n+class dynamic_caster<T*>\n+{\n+public:\n+    using result_type = T*;\n+    template <class V>\n+    T *operator()(V &v, typename std::enable_if<!std::is_polymorphic<V>::value>::type* = nullptr) const\n+    {\n+        return nullptr;\n+    }\n+    template <class V>\n+    T *operator()(V &v, typename std::enable_if<std::is_polymorphic<V>::value>::type* = nullptr) const\n+    {\n+        return dynamic_cast<T *>(&v);\n+    }\n+};\n+}\n+\n+template <class T, class V>\n+typename detail::dynamic_caster<T>::result_type\n+variant_dynamic_cast(V &v)\n+{\n+    return mapbox::util::apply_visitor(detail::dynamic_caster<T>(), v);\n+}\n+\n+template <class T, class V>\n+typename detail::dynamic_caster<const T>::result_type\n+variant_dynamic_cast(const V &v)\n+{\n+    return mapbox::util::apply_visitor(detail::dynamic_caster<const T>(), v);\n+}\n+\n+template <class T, class V>\n+T &variant_static_cast(V &v)\n+{\n+    return mapbox::util::apply_visitor(detail::static_caster<T>(), v);\n+}\n+\n+template <class T, class V>\n+const T &variant_static_cast(const V &v)\n+{\n+    return mapbox::util::apply_visitor(detail::static_caster<const T>(), v);\n+}\n+\n+}}","path":"include/mapbox/variant_cast.hpp","position":83,"original_position":83,"commit_id":"4b078bf842f166d98b6c13fac14ff3ce3e2d375f","original_commit_id":"4b078bf842f166d98b6c13fac14ff3ce3e2d375f","user":{"login":"daniel-j-h","id":527241,"avatar_url":"https://avatars0.githubusercontent.com/u/527241?v=3","gravatar_id":"","url":"https://api.github.com/users/daniel-j-h","html_url":"https://github.com/daniel-j-h","followers_url":"https://api.github.com/users/daniel-j-h/followers","following_url":"https://api.github.com/users/daniel-j-h/following{/other_user}","gists_url":"https://api.github.com/users/daniel-j-h/gists{/gist_id}","starred_url":"https://api.github.com/users/daniel-j-h/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/daniel-j-h/subscriptions","organizations_url":"https://api.github.com/users/daniel-j-h/orgs","repos_url":"https://api.github.com/users/daniel-j-h/repos","events_url":"https://api.github.com/users/daniel-j-h/events{/privacy}","received_events_url":"https://api.github.com/users/daniel-j-h/received_events","type":"User","site_admin":false},"body":"The formatting is a bit weird, did you use clang format?","created_at":"2017-04-13T20:37:52Z","updated_at":"2017-04-13T20:40:37Z","html_url":"https://github.com/mapbox/variant/pull/144#discussion_r111481499","pull_request_url":"https://api.github.com/repos/mapbox/variant/pulls/144","_links":{"self":{"href":"https://api.github.com/repos/mapbox/variant/pulls/comments/111481499"},"html":{"href":"https://github.com/mapbox/variant/pull/144#discussion_r111481499"},"pull_request":{"href":"https://api.github.com/repos/mapbox/variant/pulls/144"}}},"pull_request":{"url":"https://api.github.com/repos/mapbox/variant/pulls/144","id":115760665,"html_url":"https://github.com/mapbox/variant/pull/144","diff_url":"https://github.com/mapbox/variant/pull/144.diff","patch_url":"https://github.com/mapbox/variant/pull/144.patch","issue_url":"https://api.github.com/repos/mapbox/variant/issues/144","number":144,"state":"open","locked":false,"title":"Add variant_static_cast, variant_dynamic_cast","user":{"login":"narizhny","id":13463767,"avatar_url":"https://avatars1.githubusercontent.com/u/13463767?v=3","gravatar_id":"","url":"https://api.github.com/users/narizhny","html_url":"https://github.com/narizhny","followers_url":"https://api.github.com/users/narizhny/followers","following_url":"https://api.github.com/users/narizhny/following{/other_user}","gists_url":"https://api.github.com/users/narizhny/gists{/gist_id}","starred_url":"https://api.github.com/users/narizhny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/narizhny/subscriptions","organizations_url":"https://api.github.com/users/narizhny/orgs","repos_url":"https://api.github.com/users/narizhny/repos","events_url":"https://api.github.com/users/narizhny/events{/privacy}","received_events_url":"https://api.github.com/users/narizhny/received_events","type":"User","site_admin":false},"body":"```\r\nclass Base {public: virtual ~Base(){}};\r\nclass A: public Base {};\r\nclass B: public Base {};\r\n\r\ntypedef mapbox::util::variant<A, B> T;\r\nT t = A();\r\nauto &base = mapbox::util::variant_static_cast<Base>(t);\r\nauto &a = mapbox::util::variant_dynamic_cast<A>(t);\r\nauto &b = mapbox::util::variant_dynamic_cast<B>(t); //throws std:bad_cast\r\nauto *aPtr = mapbox::util::variant_dynamic_cast<A*>(t);\r\nauto *bPtr = mapbox::util::variant_dynamic_cast<B*>(t); //bPtr == nullptr\r\n```","created_at":"2017-04-13T13:48:41Z","updated_at":"2017-04-13T20:40:37Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d99bbd809b003a9941471a1cca22de7bc56a2991","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/mapbox/variant/pulls/144/commits","review_comments_url":"https://api.github.com/repos/mapbox/variant/pulls/144/comments","review_comment_url":"https://api.github.com/repos/mapbox/variant/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mapbox/variant/issues/144/comments","statuses_url":"https://api.github.com/repos/mapbox/variant/statuses/4b078bf842f166d98b6c13fac14ff3ce3e2d375f","head":{"label":"narizhny:Casts","ref":"Casts","sha":"4b078bf842f166d98b6c13fac14ff3ce3e2d375f","user":{"login":"narizhny","id":13463767,"avatar_url":"https://avatars1.githubusercontent.com/u/13463767?v=3","gravatar_id":"","url":"https://api.github.com/users/narizhny","html_url":"https://github.com/narizhny","followers_url":"https://api.github.com/users/narizhny/followers","following_url":"https://api.github.com/users/narizhny/following{/other_user}","gists_url":"https://api.github.com/users/narizhny/gists{/gist_id}","starred_url":"https://api.github.com/users/narizhny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/narizhny/subscriptions","organizations_url":"https://api.github.com/users/narizhny/orgs","repos_url":"https://api.github.com/users/narizhny/repos","events_url":"https://api.github.com/users/narizhny/events{/privacy}","received_events_url":"https://api.github.com/users/narizhny/received_events","type":"User","site_admin":false},"repo":{"id":88169002,"name":"variant","full_name":"narizhny/variant","owner":{"login":"narizhny","id":13463767,"avatar_url":"https://avatars1.githubusercontent.com/u/13463767?v=3","gravatar_id":"","url":"https://api.github.com/users/narizhny","html_url":"https://github.com/narizhny","followers_url":"https://api.github.com/users/narizhny/followers","following_url":"https://api.github.com/users/narizhny/following{/other_user}","gists_url":"https://api.github.com/users/narizhny/gists{/gist_id}","starred_url":"https://api.github.com/users/narizhny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/narizhny/subscriptions","organizations_url":"https://api.github.com/users/narizhny/orgs","repos_url":"https://api.github.com/users/narizhny/repos","events_url":"https://api.github.com/users/narizhny/events{/privacy}","received_events_url":"https://api.github.com/users/narizhny/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/narizhny/variant","description":"C++11/C++14 Variant","fork":true,"url":"https://api.github.com/repos/narizhny/variant","forks_url":"https://api.github.com/repos/narizhny/variant/forks","keys_url":"https://api.github.com/repos/narizhny/variant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/narizhny/variant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/narizhny/variant/teams","hooks_url":"https://api.github.com/repos/narizhny/variant/hooks","issue_events_url":"https://api.github.com/repos/narizhny/variant/issues/events{/number}","events_url":"https://api.github.com/repos/narizhny/variant/events","assignees_url":"https://api.github.com/repos/narizhny/variant/assignees{/user}","branches_url":"https://api.github.com/repos/narizhny/variant/branches{/branch}","tags_url":"https://api.github.com/repos/narizhny/variant/tags","blobs_url":"https://api.github.com/repos/narizhny/variant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/narizhny/variant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/narizhny/variant/git/refs{/sha}","trees_url":"https://api.github.com/repos/narizhny/variant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/narizhny/variant/statuses/{sha}","languages_url":"https://api.github.com/repos/narizhny/variant/languages","stargazers_url":"https://api.github.com/repos/narizhny/variant/stargazers","contributors_url":"https://api.github.com/repos/narizhny/variant/contributors","subscribers_url":"https://api.github.com/repos/narizhny/variant/subscribers","subscription_url":"https://api.github.com/repos/narizhny/variant/subscription","commits_url":"https://api.github.com/repos/narizhny/variant/commits{/sha}","git_commits_url":"https://api.github.com/repos/narizhny/variant/git/commits{/sha}","comments_url":"https://api.github.com/repos/narizhny/variant/comments{/number}","issue_comment_url":"https://api.github.com/repos/narizhny/variant/issues/comments{/number}","contents_url":"https://api.github.com/repos/narizhny/variant/contents/{+path}","compare_url":"https://api.github.com/repos/narizhny/variant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/narizhny/variant/merges","archive_url":"https://api.github.com/repos/narizhny/variant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/narizhny/variant/downloads","issues_url":"https://api.github.com/repos/narizhny/variant/issues{/number}","pulls_url":"https://api.github.com/repos/narizhny/variant/pulls{/number}","milestones_url":"https://api.github.com/repos/narizhny/variant/milestones{/number}","notifications_url":"https://api.github.com/repos/narizhny/variant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/narizhny/variant/labels{/name}","releases_url":"https://api.github.com/repos/narizhny/variant/releases{/id}","deployments_url":"https://api.github.com/repos/narizhny/variant/deployments","created_at":"2017-04-13T13:41:37Z","updated_at":"2017-04-13T13:41:39Z","pushed_at":"2017-04-13T13:44:39Z","git_url":"git://github.com/narizhny/variant.git","ssh_url":"git@github.com:narizhny/variant.git","clone_url":"https://github.com/narizhny/variant.git","svn_url":"https://github.com/narizhny/variant","homepage":"","size":610,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"mapbox:master","ref":"master","sha":"550ac2f159ca883d360c196149b466955c77a573","user":{"login":"mapbox","id":600935,"avatar_url":"https://avatars2.githubusercontent.com/u/600935?v=3","gravatar_id":"","url":"https://api.github.com/users/mapbox","html_url":"https://github.com/mapbox","followers_url":"https://api.github.com/users/mapbox/followers","following_url":"https://api.github.com/users/mapbox/following{/other_user}","gists_url":"https://api.github.com/users/mapbox/gists{/gist_id}","starred_url":"https://api.github.com/users/mapbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapbox/subscriptions","organizations_url":"https://api.github.com/users/mapbox/orgs","repos_url":"https://api.github.com/users/mapbox/repos","events_url":"https://api.github.com/users/mapbox/events{/privacy}","received_events_url":"https://api.github.com/users/mapbox/received_events","type":"Organization","site_admin":false},"repo":{"id":16744626,"name":"variant","full_name":"mapbox/variant","owner":{"login":"mapbox","id":600935,"avatar_url":"https://avatars2.githubusercontent.com/u/600935?v=3","gravatar_id":"","url":"https://api.github.com/users/mapbox","html_url":"https://github.com/mapbox","followers_url":"https://api.github.com/users/mapbox/followers","following_url":"https://api.github.com/users/mapbox/following{/other_user}","gists_url":"https://api.github.com/users/mapbox/gists{/gist_id}","starred_url":"https://api.github.com/users/mapbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mapbox/subscriptions","organizations_url":"https://api.github.com/users/mapbox/orgs","repos_url":"https://api.github.com/users/mapbox/repos","events_url":"https://api.github.com/users/mapbox/events{/privacy}","received_events_url":"https://api.github.com/users/mapbox/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mapbox/variant","description":"C++11/C++14 Variant","fork":false,"url":"https://api.github.com/repos/mapbox/variant","forks_url":"https://api.github.com/repos/mapbox/variant/forks","keys_url":"https://api.github.com/repos/mapbox/variant/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mapbox/variant/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mapbox/variant/teams","hooks_url":"https://api.github.com/repos/mapbox/variant/hooks","issue_events_url":"https://api.github.com/repos/mapbox/variant/issues/events{/number}","events_url":"https://api.github.com/repos/mapbox/variant/events","assignees_url":"https://api.github.com/repos/mapbox/variant/assignees{/user}","branches_url":"https://api.github.com/repos/mapbox/variant/branches{/branch}","tags_url":"https://api.github.com/repos/mapbox/variant/tags","blobs_url":"https://api.github.com/repos/mapbox/variant/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mapbox/variant/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mapbox/variant/git/refs{/sha}","trees_url":"https://api.github.com/repos/mapbox/variant/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mapbox/variant/statuses/{sha}","languages_url":"https://api.github.com/repos/mapbox/variant/languages","stargazers_url":"https://api.github.com/repos/mapbox/variant/stargazers","contributors_url":"https://api.github.com/repos/mapbox/variant/contributors","subscribers_url":"https://api.github.com/repos/mapbox/variant/subscribers","subscription_url":"https://api.github.com/repos/mapbox/variant/subscription","commits_url":"https://api.github.com/repos/mapbox/variant/commits{/sha}","git_commits_url":"https://api.github.com/repos/mapbox/variant/git/commits{/sha}","comments_url":"https://api.github.com/repos/mapbox/variant/comments{/number}","issue_comment_url":"https://api.github.com/repos/mapbox/variant/issues/comments{/number}","contents_url":"https://api.github.com/repos/mapbox/variant/contents/{+path}","compare_url":"https://api.github.com/repos/mapbox/variant/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mapbox/variant/merges","archive_url":"https://api.github.com/repos/mapbox/variant/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mapbox/variant/downloads","issues_url":"https://api.github.com/repos/mapbox/variant/issues{/number}","pulls_url":"https://api.github.com/repos/mapbox/variant/pulls{/number}","milestones_url":"https://api.github.com/repos/mapbox/variant/milestones{/number}","notifications_url":"https://api.github.com/repos/mapbox/variant/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mapbox/variant/labels{/name}","releases_url":"https://api.github.com/repos/mapbox/variant/releases{/id}","deployments_url":"https://api.github.com/repos/mapbox/variant/deployments","created_at":"2014-02-11T20:37:24Z","updated_at":"2017-04-10T12:07:46Z","pushed_at":"2017-04-13T13:48:41Z","git_url":"git://github.com/mapbox/variant.git","ssh_url":"git@github.com:mapbox/variant.git","clone_url":"https://github.com/mapbox/variant.git","svn_url":"https://github.com/mapbox/variant","homepage":"","size":609,"stargazers_count":112,"watchers_count":112,"language":"C++","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":35,"mirror_url":null,"open_issues_count":20,"forks":35,"open_issues":20,"watchers":112,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mapbox/variant/pulls/144"},"html":{"href":"https://github.com/mapbox/variant/pull/144"},"issue":{"href":"https://api.github.com/repos/mapbox/variant/issues/144"},"comments":{"href":"https://api.github.com/repos/mapbox/variant/issues/144/comments"},"review_comments":{"href":"https://api.github.com/repos/mapbox/variant/pulls/144/comments"},"review_comment":{"href":"https://api.github.com/repos/mapbox/variant/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mapbox/variant/pulls/144/commits"},"statuses":{"href":"https://api.github.com/repos/mapbox/variant/statuses/4b078bf842f166d98b6c13fac14ff3ce3e2d375f"}}}}
{"actor":{"display_login":"daniel-j-h"}}