Template function argument deduction with enable_if - reference to pointer












1















Maybe there is good solution that work on g++ 4.6.{3,4} ?
You can check in on https://godbolt.org/



#include <type_traits>
class A{};
class B{};
class C{
public:
A* a;
B* b;
};

template<typename T, typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type* = nullptr >
void f(T&& t) {
return;
}

int main() {
C c;
auto& cRef = c;
f(cRef.a);
f(c.a);
}


g++ /tmp/enable_if.cpp -std=c++0x



/tmp/enable_if.cpp: In function ‘int main()’:
/tmp/enable_if.cpp:20:13: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:20:13: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)
/tmp/enable_if.cpp:21:10: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:21:10: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)









share|improve this question

























  • Please keep the title as a short summary of the problem you have, not as a piece of crucial information (like showing error messages etc.). If you have a build error, then copy-paste (as text) the full and complete output into the question body.

    – Some programmer dude
    Nov 14 '18 at 11:57











  • thnx for your comments, fixed :)

    – Roma
    Nov 14 '18 at 12:06
















1















Maybe there is good solution that work on g++ 4.6.{3,4} ?
You can check in on https://godbolt.org/



#include <type_traits>
class A{};
class B{};
class C{
public:
A* a;
B* b;
};

template<typename T, typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type* = nullptr >
void f(T&& t) {
return;
}

int main() {
C c;
auto& cRef = c;
f(cRef.a);
f(c.a);
}


g++ /tmp/enable_if.cpp -std=c++0x



/tmp/enable_if.cpp: In function ‘int main()’:
/tmp/enable_if.cpp:20:13: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:20:13: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)
/tmp/enable_if.cpp:21:10: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:21:10: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)









share|improve this question

























  • Please keep the title as a short summary of the problem you have, not as a piece of crucial information (like showing error messages etc.). If you have a build error, then copy-paste (as text) the full and complete output into the question body.

    – Some programmer dude
    Nov 14 '18 at 11:57











  • thnx for your comments, fixed :)

    – Roma
    Nov 14 '18 at 12:06














1












1








1


0






Maybe there is good solution that work on g++ 4.6.{3,4} ?
You can check in on https://godbolt.org/



#include <type_traits>
class A{};
class B{};
class C{
public:
A* a;
B* b;
};

template<typename T, typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type* = nullptr >
void f(T&& t) {
return;
}

int main() {
C c;
auto& cRef = c;
f(cRef.a);
f(c.a);
}


g++ /tmp/enable_if.cpp -std=c++0x



/tmp/enable_if.cpp: In function ‘int main()’:
/tmp/enable_if.cpp:20:13: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:20:13: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)
/tmp/enable_if.cpp:21:10: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:21:10: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)









share|improve this question
















Maybe there is good solution that work on g++ 4.6.{3,4} ?
You can check in on https://godbolt.org/



#include <type_traits>
class A{};
class B{};
class C{
public:
A* a;
B* b;
};

template<typename T, typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type* = nullptr >
void f(T&& t) {
return;
}

int main() {
C c;
auto& cRef = c;
f(cRef.a);
f(c.a);
}


g++ /tmp/enable_if.cpp -std=c++0x



/tmp/enable_if.cpp: In function ‘int main()’:
/tmp/enable_if.cpp:20:13: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:20:13: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)
/tmp/enable_if.cpp:21:10: error: no matching function for call to ‘f(A*&)’
/tmp/enable_if.cpp:21:10: note: candidate is:
/tmp/enable_if.cpp:13:6: note: template<class T, typename std::enable_if<std::is_same<typename std::remove_reference<_MemPtr>::type, A*>::value, void>::type* <anonymous> > void f(T&&)






c++ c++11 c++03






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 13:08









Oktalist

10k12848




10k12848










asked Nov 14 '18 at 11:52









RomaRoma

184




184













  • Please keep the title as a short summary of the problem you have, not as a piece of crucial information (like showing error messages etc.). If you have a build error, then copy-paste (as text) the full and complete output into the question body.

    – Some programmer dude
    Nov 14 '18 at 11:57











  • thnx for your comments, fixed :)

    – Roma
    Nov 14 '18 at 12:06



















  • Please keep the title as a short summary of the problem you have, not as a piece of crucial information (like showing error messages etc.). If you have a build error, then copy-paste (as text) the full and complete output into the question body.

    – Some programmer dude
    Nov 14 '18 at 11:57











  • thnx for your comments, fixed :)

    – Roma
    Nov 14 '18 at 12:06

















Please keep the title as a short summary of the problem you have, not as a piece of crucial information (like showing error messages etc.). If you have a build error, then copy-paste (as text) the full and complete output into the question body.

– Some programmer dude
Nov 14 '18 at 11:57





Please keep the title as a short summary of the problem you have, not as a piece of crucial information (like showing error messages etc.). If you have a build error, then copy-paste (as text) the full and complete output into the question body.

– Some programmer dude
Nov 14 '18 at 11:57













thnx for your comments, fixed :)

– Roma
Nov 14 '18 at 12:06





thnx for your comments, fixed :)

– Roma
Nov 14 '18 at 12:06












1 Answer
1






active

oldest

votes


















6














The feature that allows you to default a function template parameter was introduced in C++11. Your compiler actually does not have a full support for this feature. As a workaround, you can put std::enable_if as a function return type:



template<typename T >
typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type f(T&& t) {
return;
}





share|improve this answer
























  • thnx, out actual function return type is bool, but putting enable_if in function arguments also work.

    – Roma
    Nov 14 '18 at 13:15








  • 4





    @Roma - You can specify a second argument to enable_if, that it should expose as ::type. So you can put it in the return type even if you need to return bool.

    – StoryTeller
    Nov 14 '18 at 13:32











  • @StoryTeller , o, thnx !!! good to know :)

    – Roma
    Nov 14 '18 at 13:41











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299635%2ftemplate-function-argument-deduction-with-enable-if-reference-to-pointer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









6














The feature that allows you to default a function template parameter was introduced in C++11. Your compiler actually does not have a full support for this feature. As a workaround, you can put std::enable_if as a function return type:



template<typename T >
typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type f(T&& t) {
return;
}





share|improve this answer
























  • thnx, out actual function return type is bool, but putting enable_if in function arguments also work.

    – Roma
    Nov 14 '18 at 13:15








  • 4





    @Roma - You can specify a second argument to enable_if, that it should expose as ::type. So you can put it in the return type even if you need to return bool.

    – StoryTeller
    Nov 14 '18 at 13:32











  • @StoryTeller , o, thnx !!! good to know :)

    – Roma
    Nov 14 '18 at 13:41
















6














The feature that allows you to default a function template parameter was introduced in C++11. Your compiler actually does not have a full support for this feature. As a workaround, you can put std::enable_if as a function return type:



template<typename T >
typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type f(T&& t) {
return;
}





share|improve this answer
























  • thnx, out actual function return type is bool, but putting enable_if in function arguments also work.

    – Roma
    Nov 14 '18 at 13:15








  • 4





    @Roma - You can specify a second argument to enable_if, that it should expose as ::type. So you can put it in the return type even if you need to return bool.

    – StoryTeller
    Nov 14 '18 at 13:32











  • @StoryTeller , o, thnx !!! good to know :)

    – Roma
    Nov 14 '18 at 13:41














6












6








6







The feature that allows you to default a function template parameter was introduced in C++11. Your compiler actually does not have a full support for this feature. As a workaround, you can put std::enable_if as a function return type:



template<typename T >
typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type f(T&& t) {
return;
}





share|improve this answer













The feature that allows you to default a function template parameter was introduced in C++11. Your compiler actually does not have a full support for this feature. As a workaround, you can put std::enable_if as a function return type:



template<typename T >
typename std::enable_if<std::is_same<typename std::remove_reference<T>::type,A*>::value>::type f(T&& t) {
return;
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 13:03









Rafał GórczewskiRafał Górczewski

1517




1517













  • thnx, out actual function return type is bool, but putting enable_if in function arguments also work.

    – Roma
    Nov 14 '18 at 13:15








  • 4





    @Roma - You can specify a second argument to enable_if, that it should expose as ::type. So you can put it in the return type even if you need to return bool.

    – StoryTeller
    Nov 14 '18 at 13:32











  • @StoryTeller , o, thnx !!! good to know :)

    – Roma
    Nov 14 '18 at 13:41



















  • thnx, out actual function return type is bool, but putting enable_if in function arguments also work.

    – Roma
    Nov 14 '18 at 13:15








  • 4





    @Roma - You can specify a second argument to enable_if, that it should expose as ::type. So you can put it in the return type even if you need to return bool.

    – StoryTeller
    Nov 14 '18 at 13:32











  • @StoryTeller , o, thnx !!! good to know :)

    – Roma
    Nov 14 '18 at 13:41

















thnx, out actual function return type is bool, but putting enable_if in function arguments also work.

– Roma
Nov 14 '18 at 13:15







thnx, out actual function return type is bool, but putting enable_if in function arguments also work.

– Roma
Nov 14 '18 at 13:15






4




4





@Roma - You can specify a second argument to enable_if, that it should expose as ::type. So you can put it in the return type even if you need to return bool.

– StoryTeller
Nov 14 '18 at 13:32





@Roma - You can specify a second argument to enable_if, that it should expose as ::type. So you can put it in the return type even if you need to return bool.

– StoryTeller
Nov 14 '18 at 13:32













@StoryTeller , o, thnx !!! good to know :)

– Roma
Nov 14 '18 at 13:41





@StoryTeller , o, thnx !!! good to know :)

– Roma
Nov 14 '18 at 13:41


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299635%2ftemplate-function-argument-deduction-with-enable-if-reference-to-pointer%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python