Any 'design pattern' of c++ enable_if and SFINAE?
I did quite some searches on Internet and I found some examples of enable_if in different contexts.
However, pretty much all of the examples I found are 'dummy' examples, which can be done easily through inheritance and other OO approaches.
I am wondering what's the real world application of enable_if, the one that is difficult to be implemented in OO.
Or in other words, in what kind of application enable_if could be most useful/elegant?
Can anyone provide an example here?
I can understand c++ std library uses a lot of enable_if, but I have no intension to write a std library.
I am currently working on fleet management software. I am wondering if there is any kind of 'pattern' for using enable_if in software design.
For example, enable_if can be used to 'differentiate' return type:
template<typename T>
std::enable_if_t<std::is_integral<T>::value> f(T t){
//integral version
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value> f(T t){
//floating point version
}
Thanks a lot
c++ metaprogramming
add a comment |
I did quite some searches on Internet and I found some examples of enable_if in different contexts.
However, pretty much all of the examples I found are 'dummy' examples, which can be done easily through inheritance and other OO approaches.
I am wondering what's the real world application of enable_if, the one that is difficult to be implemented in OO.
Or in other words, in what kind of application enable_if could be most useful/elegant?
Can anyone provide an example here?
I can understand c++ std library uses a lot of enable_if, but I have no intension to write a std library.
I am currently working on fleet management software. I am wondering if there is any kind of 'pattern' for using enable_if in software design.
For example, enable_if can be used to 'differentiate' return type:
template<typename T>
std::enable_if_t<std::is_integral<T>::value> f(T t){
//integral version
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value> f(T t){
//floating point version
}
Thanks a lot
c++ metaprogramming
std::begin
comes to mind. Your question is too broad though, have a look at the standard library, as it is used in the "real world".
– Rakete1111
Mar 16 '17 at 21:18
You can find real life examples in the C++ standard library and many of the boost libraries.
– juanchopanza
Mar 16 '17 at 21:19
thanks for the comments. In stead of writing your own library, is there any other scenario where enable_if should be used?
– user152503
Mar 16 '17 at 21:21
You might get something from the introduction of boost::hana. In my opinion, meta-programming likes creating (type-safe) mods for c++...
– felix
Mar 16 '17 at 22:35
enable_if
shows up in library/template code. In straightforward application code, you typically know whether a function is needed.
– MSalters
Mar 17 '17 at 8:11
add a comment |
I did quite some searches on Internet and I found some examples of enable_if in different contexts.
However, pretty much all of the examples I found are 'dummy' examples, which can be done easily through inheritance and other OO approaches.
I am wondering what's the real world application of enable_if, the one that is difficult to be implemented in OO.
Or in other words, in what kind of application enable_if could be most useful/elegant?
Can anyone provide an example here?
I can understand c++ std library uses a lot of enable_if, but I have no intension to write a std library.
I am currently working on fleet management software. I am wondering if there is any kind of 'pattern' for using enable_if in software design.
For example, enable_if can be used to 'differentiate' return type:
template<typename T>
std::enable_if_t<std::is_integral<T>::value> f(T t){
//integral version
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value> f(T t){
//floating point version
}
Thanks a lot
c++ metaprogramming
I did quite some searches on Internet and I found some examples of enable_if in different contexts.
However, pretty much all of the examples I found are 'dummy' examples, which can be done easily through inheritance and other OO approaches.
I am wondering what's the real world application of enable_if, the one that is difficult to be implemented in OO.
Or in other words, in what kind of application enable_if could be most useful/elegant?
Can anyone provide an example here?
I can understand c++ std library uses a lot of enable_if, but I have no intension to write a std library.
I am currently working on fleet management software. I am wondering if there is any kind of 'pattern' for using enable_if in software design.
For example, enable_if can be used to 'differentiate' return type:
template<typename T>
std::enable_if_t<std::is_integral<T>::value> f(T t){
//integral version
}
template<typename T>
std::enable_if_t<std::is_floating_point<T>::value> f(T t){
//floating point version
}
Thanks a lot
c++ metaprogramming
c++ metaprogramming
edited Mar 16 '17 at 21:33
user152503
asked Mar 16 '17 at 21:14
user152503user152503
231112
231112
std::begin
comes to mind. Your question is too broad though, have a look at the standard library, as it is used in the "real world".
– Rakete1111
Mar 16 '17 at 21:18
You can find real life examples in the C++ standard library and many of the boost libraries.
– juanchopanza
Mar 16 '17 at 21:19
thanks for the comments. In stead of writing your own library, is there any other scenario where enable_if should be used?
– user152503
Mar 16 '17 at 21:21
You might get something from the introduction of boost::hana. In my opinion, meta-programming likes creating (type-safe) mods for c++...
– felix
Mar 16 '17 at 22:35
enable_if
shows up in library/template code. In straightforward application code, you typically know whether a function is needed.
– MSalters
Mar 17 '17 at 8:11
add a comment |
std::begin
comes to mind. Your question is too broad though, have a look at the standard library, as it is used in the "real world".
– Rakete1111
Mar 16 '17 at 21:18
You can find real life examples in the C++ standard library and many of the boost libraries.
– juanchopanza
Mar 16 '17 at 21:19
thanks for the comments. In stead of writing your own library, is there any other scenario where enable_if should be used?
– user152503
Mar 16 '17 at 21:21
You might get something from the introduction of boost::hana. In my opinion, meta-programming likes creating (type-safe) mods for c++...
– felix
Mar 16 '17 at 22:35
enable_if
shows up in library/template code. In straightforward application code, you typically know whether a function is needed.
– MSalters
Mar 17 '17 at 8:11
std::begin
comes to mind. Your question is too broad though, have a look at the standard library, as it is used in the "real world".– Rakete1111
Mar 16 '17 at 21:18
std::begin
comes to mind. Your question is too broad though, have a look at the standard library, as it is used in the "real world".– Rakete1111
Mar 16 '17 at 21:18
You can find real life examples in the C++ standard library and many of the boost libraries.
– juanchopanza
Mar 16 '17 at 21:19
You can find real life examples in the C++ standard library and many of the boost libraries.
– juanchopanza
Mar 16 '17 at 21:19
thanks for the comments. In stead of writing your own library, is there any other scenario where enable_if should be used?
– user152503
Mar 16 '17 at 21:21
thanks for the comments. In stead of writing your own library, is there any other scenario where enable_if should be used?
– user152503
Mar 16 '17 at 21:21
You might get something from the introduction of boost::hana. In my opinion, meta-programming likes creating (type-safe) mods for c++...
– felix
Mar 16 '17 at 22:35
You might get something from the introduction of boost::hana. In my opinion, meta-programming likes creating (type-safe) mods for c++...
– felix
Mar 16 '17 at 22:35
enable_if
shows up in library/template code. In straightforward application code, you typically know whether a function is needed.– MSalters
Mar 17 '17 at 8:11
enable_if
shows up in library/template code. In straightforward application code, you typically know whether a function is needed.– MSalters
Mar 17 '17 at 8:11
add a comment |
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%2f42844894%2fany-design-pattern-of-c-enable-if-and-sfinae%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%2f42844894%2fany-design-pattern-of-c-enable-if-and-sfinae%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
std::begin
comes to mind. Your question is too broad though, have a look at the standard library, as it is used in the "real world".– Rakete1111
Mar 16 '17 at 21:18
You can find real life examples in the C++ standard library and many of the boost libraries.
– juanchopanza
Mar 16 '17 at 21:19
thanks for the comments. In stead of writing your own library, is there any other scenario where enable_if should be used?
– user152503
Mar 16 '17 at 21:21
You might get something from the introduction of boost::hana. In my opinion, meta-programming likes creating (type-safe) mods for c++...
– felix
Mar 16 '17 at 22:35
enable_if
shows up in library/template code. In straightforward application code, you typically know whether a function is needed.– MSalters
Mar 17 '17 at 8:11