Calling methods of temporary objects created using class template argument deduction [duplicate]












7
















This question already has an answer here:




  • Template Argument Deduction Broken in Clang 6 for Temporary Objects

    1 answer




I have the following piece of C++17 code that uses class template deduction:



template <typename T>
struct Test {
T t;
Test(T t) : t(t) {}
bool check() { return true; }
};

template <typename T>
bool check(T t) {
return Test(t).check();
}

int main() {
return check(1);
}


gcc 8.2 compiles this without any problems, while clang 7.0 complains:



test.cpp:10:16: error: member reference base type 'Test' is not a structure or union
return Test(t).check();
~~~~~~~^~~~~~


I do not yet have a complete grasp of the intricacies of the class template argument deduction mechanism. Is this a bug in clang or am I using CTAD in a wrong way?










share|improve this question















marked as duplicate by Jans, Matthieu Brucher, πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 17:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    I can confirm the error, with clang++ -std=c++17.

    – lubgr
    Nov 22 '18 at 17:03






  • 2





    Works on latest GCC and MSVC (with c++17 switch), so I assume this is a clang issue. Don't forget to create a bug report.

    – Dan M.
    Nov 22 '18 at 17:05








  • 1





    Yes the issue is clang: godbolt.org/z/bg5cuN

    – Oliv
    Nov 22 '18 at 17:05











  • How can this be a duplicate of a question about a bug that has already been resolved in clang? This seems to be a different bug.

    – Nikola Benes
    Nov 22 '18 at 18:10






  • 1





    The bug report: llvm.org/pr39663

    – Rakete1111
    Nov 23 '18 at 6:19
















7
















This question already has an answer here:




  • Template Argument Deduction Broken in Clang 6 for Temporary Objects

    1 answer




I have the following piece of C++17 code that uses class template deduction:



template <typename T>
struct Test {
T t;
Test(T t) : t(t) {}
bool check() { return true; }
};

template <typename T>
bool check(T t) {
return Test(t).check();
}

int main() {
return check(1);
}


gcc 8.2 compiles this without any problems, while clang 7.0 complains:



test.cpp:10:16: error: member reference base type 'Test' is not a structure or union
return Test(t).check();
~~~~~~~^~~~~~


I do not yet have a complete grasp of the intricacies of the class template argument deduction mechanism. Is this a bug in clang or am I using CTAD in a wrong way?










share|improve this question















marked as duplicate by Jans, Matthieu Brucher, πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 17:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    I can confirm the error, with clang++ -std=c++17.

    – lubgr
    Nov 22 '18 at 17:03






  • 2





    Works on latest GCC and MSVC (with c++17 switch), so I assume this is a clang issue. Don't forget to create a bug report.

    – Dan M.
    Nov 22 '18 at 17:05








  • 1





    Yes the issue is clang: godbolt.org/z/bg5cuN

    – Oliv
    Nov 22 '18 at 17:05











  • How can this be a duplicate of a question about a bug that has already been resolved in clang? This seems to be a different bug.

    – Nikola Benes
    Nov 22 '18 at 18:10






  • 1





    The bug report: llvm.org/pr39663

    – Rakete1111
    Nov 23 '18 at 6:19














7












7








7









This question already has an answer here:




  • Template Argument Deduction Broken in Clang 6 for Temporary Objects

    1 answer




I have the following piece of C++17 code that uses class template deduction:



template <typename T>
struct Test {
T t;
Test(T t) : t(t) {}
bool check() { return true; }
};

template <typename T>
bool check(T t) {
return Test(t).check();
}

int main() {
return check(1);
}


gcc 8.2 compiles this without any problems, while clang 7.0 complains:



test.cpp:10:16: error: member reference base type 'Test' is not a structure or union
return Test(t).check();
~~~~~~~^~~~~~


I do not yet have a complete grasp of the intricacies of the class template argument deduction mechanism. Is this a bug in clang or am I using CTAD in a wrong way?










share|improve this question

















This question already has an answer here:




  • Template Argument Deduction Broken in Clang 6 for Temporary Objects

    1 answer




I have the following piece of C++17 code that uses class template deduction:



template <typename T>
struct Test {
T t;
Test(T t) : t(t) {}
bool check() { return true; }
};

template <typename T>
bool check(T t) {
return Test(t).check();
}

int main() {
return check(1);
}


gcc 8.2 compiles this without any problems, while clang 7.0 complains:



test.cpp:10:16: error: member reference base type 'Test' is not a structure or union
return Test(t).check();
~~~~~~~^~~~~~


I do not yet have a complete grasp of the intricacies of the class template argument deduction mechanism. Is this a bug in clang or am I using CTAD in a wrong way?





This question already has an answer here:




  • Template Argument Deduction Broken in Clang 6 for Temporary Objects

    1 answer








c++ gcc clang language-lawyer c++17






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 17:01









Nicol Bolas

285k33471645




285k33471645










asked Nov 22 '18 at 16:59









Nikola BenesNikola Benes

96811022




96811022




marked as duplicate by Jans, Matthieu Brucher, πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 17:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Jans, Matthieu Brucher, πάντα ῥεῖ c++
Users with the  c++ badge can single-handedly close c++ questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 17:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    I can confirm the error, with clang++ -std=c++17.

    – lubgr
    Nov 22 '18 at 17:03






  • 2





    Works on latest GCC and MSVC (with c++17 switch), so I assume this is a clang issue. Don't forget to create a bug report.

    – Dan M.
    Nov 22 '18 at 17:05








  • 1





    Yes the issue is clang: godbolt.org/z/bg5cuN

    – Oliv
    Nov 22 '18 at 17:05











  • How can this be a duplicate of a question about a bug that has already been resolved in clang? This seems to be a different bug.

    – Nikola Benes
    Nov 22 '18 at 18:10






  • 1





    The bug report: llvm.org/pr39663

    – Rakete1111
    Nov 23 '18 at 6:19














  • 1





    I can confirm the error, with clang++ -std=c++17.

    – lubgr
    Nov 22 '18 at 17:03






  • 2





    Works on latest GCC and MSVC (with c++17 switch), so I assume this is a clang issue. Don't forget to create a bug report.

    – Dan M.
    Nov 22 '18 at 17:05








  • 1





    Yes the issue is clang: godbolt.org/z/bg5cuN

    – Oliv
    Nov 22 '18 at 17:05











  • How can this be a duplicate of a question about a bug that has already been resolved in clang? This seems to be a different bug.

    – Nikola Benes
    Nov 22 '18 at 18:10






  • 1





    The bug report: llvm.org/pr39663

    – Rakete1111
    Nov 23 '18 at 6:19








1




1





I can confirm the error, with clang++ -std=c++17.

– lubgr
Nov 22 '18 at 17:03





I can confirm the error, with clang++ -std=c++17.

– lubgr
Nov 22 '18 at 17:03




2




2





Works on latest GCC and MSVC (with c++17 switch), so I assume this is a clang issue. Don't forget to create a bug report.

– Dan M.
Nov 22 '18 at 17:05







Works on latest GCC and MSVC (with c++17 switch), so I assume this is a clang issue. Don't forget to create a bug report.

– Dan M.
Nov 22 '18 at 17:05






1




1





Yes the issue is clang: godbolt.org/z/bg5cuN

– Oliv
Nov 22 '18 at 17:05





Yes the issue is clang: godbolt.org/z/bg5cuN

– Oliv
Nov 22 '18 at 17:05













How can this be a duplicate of a question about a bug that has already been resolved in clang? This seems to be a different bug.

– Nikola Benes
Nov 22 '18 at 18:10





How can this be a duplicate of a question about a bug that has already been resolved in clang? This seems to be a different bug.

– Nikola Benes
Nov 22 '18 at 18:10




1




1





The bug report: llvm.org/pr39663

– Rakete1111
Nov 23 '18 at 6:19





The bug report: llvm.org/pr39663

– Rakete1111
Nov 23 '18 at 6:19












1 Answer
1






active

oldest

votes


















4














This is a clang bug [expr.type.conv]/1:




If the type is a placeholder for a deduced class type, it is replaced by the return type of the function selected by overload resolution for class template deduction for the remainder of this subclause.




So template deduction also applies in functional conversion expression.



You can circumvent this clang bug this way:



template <typename T>
bool check(T t) {
auto x=Test(t);
return x.check();
}





share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    This is a clang bug [expr.type.conv]/1:




    If the type is a placeholder for a deduced class type, it is replaced by the return type of the function selected by overload resolution for class template deduction for the remainder of this subclause.




    So template deduction also applies in functional conversion expression.



    You can circumvent this clang bug this way:



    template <typename T>
    bool check(T t) {
    auto x=Test(t);
    return x.check();
    }





    share|improve this answer




























      4














      This is a clang bug [expr.type.conv]/1:




      If the type is a placeholder for a deduced class type, it is replaced by the return type of the function selected by overload resolution for class template deduction for the remainder of this subclause.




      So template deduction also applies in functional conversion expression.



      You can circumvent this clang bug this way:



      template <typename T>
      bool check(T t) {
      auto x=Test(t);
      return x.check();
      }





      share|improve this answer


























        4












        4








        4







        This is a clang bug [expr.type.conv]/1:




        If the type is a placeholder for a deduced class type, it is replaced by the return type of the function selected by overload resolution for class template deduction for the remainder of this subclause.




        So template deduction also applies in functional conversion expression.



        You can circumvent this clang bug this way:



        template <typename T>
        bool check(T t) {
        auto x=Test(t);
        return x.check();
        }





        share|improve this answer













        This is a clang bug [expr.type.conv]/1:




        If the type is a placeholder for a deduced class type, it is replaced by the return type of the function selected by overload resolution for class template deduction for the remainder of this subclause.




        So template deduction also applies in functional conversion expression.



        You can circumvent this clang bug this way:



        template <typename T>
        bool check(T t) {
        auto x=Test(t);
        return x.check();
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 17:12









        OlivOliv

        9,1021957




        9,1021957















            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)