why does type_identity break the implementation of is_detected
p0887r1:
2.3 Fundamental metafunction building block
There are two ubiquitous idioms for type traits:
- define a public data member value with a given value
- define a public member typedef type that names a given type
It is surprising that there is a standard utility providing the former (
std::integral_constant),
but no standard utility providing the latter.
type_identityis this utility. It is a fundamental building block that other metafunctions can
simply inherit from. For example,remove_constcould be implemented as follows:
template <typename T>
struct remove_const : type_identity<T> {};
template <typename T>
struct remove_const<T const> : type_identity<T> {};
Its implementation is simple:
template<class T>
struct type_identity
{
using type = T;
};
So, I try to use type_identity widely in my codes, including personal implementation of Detection Idiom:
namespace detail
{
template<class Default,
class AlwaysVoid,
template<class...>
class Op,
class... Args>
struct detector : type_identity<Default> // here
{
using value_t = std::false_type;
};
template<class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
: type_identity<Op<Args...>> // here
{
using value_t = std::true_type;
};
} // namespace detail
// ......
It works fine everywhere until I used libcxx's testsuits for my own implementation of is_destructible, below is the failure case:
struct ProtectedDestructor
{
protected:
~ProtectedDestructor()
{}
};
// ......
template<class T>
void
test_is_not_destructible()
{
static_assert(!is_destructible<T>::value, "");
// ......
}
// ......
test_is_not_destructible<ProtectedDestructor>();
live demo:
prog.cc:83:47: error: '~ProtectedDestructor' is a protected member of 'ProtectedDestructor'
using has_dtor = decltype(std::declval().~U());
^
prog.cc:26:19: note: in instantiation of template type alias 'has_dtor' requested here
: type_identity<Op<Args...>>
^
prog.cc:45:1: note: in instantiation of template class 'detail::detector
......
It's werid that once replace type_identity with trival using type = ......, the compiler has no error, demo. For other trival has_member check, type_identity works fine, demo.
So, the only problem here is, for protected dtor, type_identity will force struct detail::detector to check the validity of dtor, while using type = something will not.
I think the solution is simple, just remove type_identity, and use using type = something directly, just like Walter E. Brown's original implementation. But the question is:
why does type_idintity break here, while trival using type = something not?
c++ c++17 identity template-meta-programming detection-idiom
|
show 3 more comments
p0887r1:
2.3 Fundamental metafunction building block
There are two ubiquitous idioms for type traits:
- define a public data member value with a given value
- define a public member typedef type that names a given type
It is surprising that there is a standard utility providing the former (
std::integral_constant),
but no standard utility providing the latter.
type_identityis this utility. It is a fundamental building block that other metafunctions can
simply inherit from. For example,remove_constcould be implemented as follows:
template <typename T>
struct remove_const : type_identity<T> {};
template <typename T>
struct remove_const<T const> : type_identity<T> {};
Its implementation is simple:
template<class T>
struct type_identity
{
using type = T;
};
So, I try to use type_identity widely in my codes, including personal implementation of Detection Idiom:
namespace detail
{
template<class Default,
class AlwaysVoid,
template<class...>
class Op,
class... Args>
struct detector : type_identity<Default> // here
{
using value_t = std::false_type;
};
template<class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
: type_identity<Op<Args...>> // here
{
using value_t = std::true_type;
};
} // namespace detail
// ......
It works fine everywhere until I used libcxx's testsuits for my own implementation of is_destructible, below is the failure case:
struct ProtectedDestructor
{
protected:
~ProtectedDestructor()
{}
};
// ......
template<class T>
void
test_is_not_destructible()
{
static_assert(!is_destructible<T>::value, "");
// ......
}
// ......
test_is_not_destructible<ProtectedDestructor>();
live demo:
prog.cc:83:47: error: '~ProtectedDestructor' is a protected member of 'ProtectedDestructor'
using has_dtor = decltype(std::declval().~U());
^
prog.cc:26:19: note: in instantiation of template type alias 'has_dtor' requested here
: type_identity<Op<Args...>>
^
prog.cc:45:1: note: in instantiation of template class 'detail::detector
......
It's werid that once replace type_identity with trival using type = ......, the compiler has no error, demo. For other trival has_member check, type_identity works fine, demo.
So, the only problem here is, for protected dtor, type_identity will force struct detail::detector to check the validity of dtor, while using type = something will not.
I think the solution is simple, just remove type_identity, and use using type = something directly, just like Walter E. Brown's original implementation. But the question is:
why does type_idintity break here, while trival using type = something not?
c++ c++17 identity template-meta-programming detection-idiom
2
This looks like a clang bug to me. It compiles fine with GCC if you use myis_destructibleimplementation from your previous question. BTW, you should create a Minimal, Complete, and Verifiable example to ask a language-lawyer question, there are too much irrelevant code here.
– llllllllll
Nov 25 '18 at 10:02
particularly for questions related to type-traits and tamplate-meta-programming, please, ever explicit the exact version of the standard. C++17, in this case, I suppose.
– max66
Nov 25 '18 at 10:22
@liliscent Sorry, didn't test on gcc and remove unnecessary codes/simplify my codes are my bad.
– 陳 力
Nov 25 '18 at 10:22
@max66 on stackoverfow, I saw someone has said the tagc++indicates the latest released version.type_identityis available in c++20. I'll tag it later
– 陳 力
Nov 25 '18 at 10:26
I'll report bugs to gcc and clang later
– 陳 力
Nov 25 '18 at 10:28
|
show 3 more comments
p0887r1:
2.3 Fundamental metafunction building block
There are two ubiquitous idioms for type traits:
- define a public data member value with a given value
- define a public member typedef type that names a given type
It is surprising that there is a standard utility providing the former (
std::integral_constant),
but no standard utility providing the latter.
type_identityis this utility. It is a fundamental building block that other metafunctions can
simply inherit from. For example,remove_constcould be implemented as follows:
template <typename T>
struct remove_const : type_identity<T> {};
template <typename T>
struct remove_const<T const> : type_identity<T> {};
Its implementation is simple:
template<class T>
struct type_identity
{
using type = T;
};
So, I try to use type_identity widely in my codes, including personal implementation of Detection Idiom:
namespace detail
{
template<class Default,
class AlwaysVoid,
template<class...>
class Op,
class... Args>
struct detector : type_identity<Default> // here
{
using value_t = std::false_type;
};
template<class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
: type_identity<Op<Args...>> // here
{
using value_t = std::true_type;
};
} // namespace detail
// ......
It works fine everywhere until I used libcxx's testsuits for my own implementation of is_destructible, below is the failure case:
struct ProtectedDestructor
{
protected:
~ProtectedDestructor()
{}
};
// ......
template<class T>
void
test_is_not_destructible()
{
static_assert(!is_destructible<T>::value, "");
// ......
}
// ......
test_is_not_destructible<ProtectedDestructor>();
live demo:
prog.cc:83:47: error: '~ProtectedDestructor' is a protected member of 'ProtectedDestructor'
using has_dtor = decltype(std::declval().~U());
^
prog.cc:26:19: note: in instantiation of template type alias 'has_dtor' requested here
: type_identity<Op<Args...>>
^
prog.cc:45:1: note: in instantiation of template class 'detail::detector
......
It's werid that once replace type_identity with trival using type = ......, the compiler has no error, demo. For other trival has_member check, type_identity works fine, demo.
So, the only problem here is, for protected dtor, type_identity will force struct detail::detector to check the validity of dtor, while using type = something will not.
I think the solution is simple, just remove type_identity, and use using type = something directly, just like Walter E. Brown's original implementation. But the question is:
why does type_idintity break here, while trival using type = something not?
c++ c++17 identity template-meta-programming detection-idiom
p0887r1:
2.3 Fundamental metafunction building block
There are two ubiquitous idioms for type traits:
- define a public data member value with a given value
- define a public member typedef type that names a given type
It is surprising that there is a standard utility providing the former (
std::integral_constant),
but no standard utility providing the latter.
type_identityis this utility. It is a fundamental building block that other metafunctions can
simply inherit from. For example,remove_constcould be implemented as follows:
template <typename T>
struct remove_const : type_identity<T> {};
template <typename T>
struct remove_const<T const> : type_identity<T> {};
Its implementation is simple:
template<class T>
struct type_identity
{
using type = T;
};
So, I try to use type_identity widely in my codes, including personal implementation of Detection Idiom:
namespace detail
{
template<class Default,
class AlwaysVoid,
template<class...>
class Op,
class... Args>
struct detector : type_identity<Default> // here
{
using value_t = std::false_type;
};
template<class Default, template<class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
: type_identity<Op<Args...>> // here
{
using value_t = std::true_type;
};
} // namespace detail
// ......
It works fine everywhere until I used libcxx's testsuits for my own implementation of is_destructible, below is the failure case:
struct ProtectedDestructor
{
protected:
~ProtectedDestructor()
{}
};
// ......
template<class T>
void
test_is_not_destructible()
{
static_assert(!is_destructible<T>::value, "");
// ......
}
// ......
test_is_not_destructible<ProtectedDestructor>();
live demo:
prog.cc:83:47: error: '~ProtectedDestructor' is a protected member of 'ProtectedDestructor'
using has_dtor = decltype(std::declval().~U());
^
prog.cc:26:19: note: in instantiation of template type alias 'has_dtor' requested here
: type_identity<Op<Args...>>
^
prog.cc:45:1: note: in instantiation of template class 'detail::detector
......
It's werid that once replace type_identity with trival using type = ......, the compiler has no error, demo. For other trival has_member check, type_identity works fine, demo.
So, the only problem here is, for protected dtor, type_identity will force struct detail::detector to check the validity of dtor, while using type = something will not.
I think the solution is simple, just remove type_identity, and use using type = something directly, just like Walter E. Brown's original implementation. But the question is:
why does type_idintity break here, while trival using type = something not?
c++ c++17 identity template-meta-programming detection-idiom
c++ c++17 identity template-meta-programming detection-idiom
edited Nov 26 '18 at 1:06
陳 力
asked Nov 25 '18 at 8:16
陳 力陳 力
1,7561724
1,7561724
2
This looks like a clang bug to me. It compiles fine with GCC if you use myis_destructibleimplementation from your previous question. BTW, you should create a Minimal, Complete, and Verifiable example to ask a language-lawyer question, there are too much irrelevant code here.
– llllllllll
Nov 25 '18 at 10:02
particularly for questions related to type-traits and tamplate-meta-programming, please, ever explicit the exact version of the standard. C++17, in this case, I suppose.
– max66
Nov 25 '18 at 10:22
@liliscent Sorry, didn't test on gcc and remove unnecessary codes/simplify my codes are my bad.
– 陳 力
Nov 25 '18 at 10:22
@max66 on stackoverfow, I saw someone has said the tagc++indicates the latest released version.type_identityis available in c++20. I'll tag it later
– 陳 力
Nov 25 '18 at 10:26
I'll report bugs to gcc and clang later
– 陳 力
Nov 25 '18 at 10:28
|
show 3 more comments
2
This looks like a clang bug to me. It compiles fine with GCC if you use myis_destructibleimplementation from your previous question. BTW, you should create a Minimal, Complete, and Verifiable example to ask a language-lawyer question, there are too much irrelevant code here.
– llllllllll
Nov 25 '18 at 10:02
particularly for questions related to type-traits and tamplate-meta-programming, please, ever explicit the exact version of the standard. C++17, in this case, I suppose.
– max66
Nov 25 '18 at 10:22
@liliscent Sorry, didn't test on gcc and remove unnecessary codes/simplify my codes are my bad.
– 陳 力
Nov 25 '18 at 10:22
@max66 on stackoverfow, I saw someone has said the tagc++indicates the latest released version.type_identityis available in c++20. I'll tag it later
– 陳 力
Nov 25 '18 at 10:26
I'll report bugs to gcc and clang later
– 陳 力
Nov 25 '18 at 10:28
2
2
This looks like a clang bug to me. It compiles fine with GCC if you use my
is_destructible implementation from your previous question. BTW, you should create a Minimal, Complete, and Verifiable example to ask a language-lawyer question, there are too much irrelevant code here.– llllllllll
Nov 25 '18 at 10:02
This looks like a clang bug to me. It compiles fine with GCC if you use my
is_destructible implementation from your previous question. BTW, you should create a Minimal, Complete, and Verifiable example to ask a language-lawyer question, there are too much irrelevant code here.– llllllllll
Nov 25 '18 at 10:02
particularly for questions related to type-traits and tamplate-meta-programming, please, ever explicit the exact version of the standard. C++17, in this case, I suppose.
– max66
Nov 25 '18 at 10:22
particularly for questions related to type-traits and tamplate-meta-programming, please, ever explicit the exact version of the standard. C++17, in this case, I suppose.
– max66
Nov 25 '18 at 10:22
@liliscent Sorry, didn't test on gcc and remove unnecessary codes/simplify my codes are my bad.
– 陳 力
Nov 25 '18 at 10:22
@liliscent Sorry, didn't test on gcc and remove unnecessary codes/simplify my codes are my bad.
– 陳 力
Nov 25 '18 at 10:22
@max66 on stackoverfow, I saw someone has said the tag
c++ indicates the latest released version. type_identity is available in c++20. I'll tag it later– 陳 力
Nov 25 '18 at 10:26
@max66 on stackoverfow, I saw someone has said the tag
c++ indicates the latest released version. type_identity is available in c++20. I'll tag it later– 陳 力
Nov 25 '18 at 10:26
I'll report bugs to gcc and clang later
– 陳 力
Nov 25 '18 at 10:28
I'll report bugs to gcc and clang later
– 陳 力
Nov 25 '18 at 10:28
|
show 3 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465765%2fwhy-does-type-identity-break-the-implementation-of-is-detected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465765%2fwhy-does-type-identity-break-the-implementation-of-is-detected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
This looks like a clang bug to me. It compiles fine with GCC if you use my
is_destructibleimplementation from your previous question. BTW, you should create a Minimal, Complete, and Verifiable example to ask a language-lawyer question, there are too much irrelevant code here.– llllllllll
Nov 25 '18 at 10:02
particularly for questions related to type-traits and tamplate-meta-programming, please, ever explicit the exact version of the standard. C++17, in this case, I suppose.
– max66
Nov 25 '18 at 10:22
@liliscent Sorry, didn't test on gcc and remove unnecessary codes/simplify my codes are my bad.
– 陳 力
Nov 25 '18 at 10:22
@max66 on stackoverfow, I saw someone has said the tag
c++indicates the latest released version.type_identityis available in c++20. I'll tag it later– 陳 力
Nov 25 '18 at 10:26
I'll report bugs to gcc and clang later
– 陳 力
Nov 25 '18 at 10:28