public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/61636] New: generic lambda  "cannot call member function without object"
@ 2014-06-28  8:13 tower120 at gmail dot com
  2014-06-28  8:22 ` [Bug c++/61636] " tower120 at gmail dot com
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: tower120 at gmail dot com @ 2014-06-28  8:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61636

            Bug ID: 61636
           Summary: generic lambda  "cannot call member function without
                    object"
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tower120 at gmail dot com

In the following code:
Live http://coliru.stacked-crooked.com/a/b22bb3c9cd603bc7



#include <type_traits>
#include <iostream>

template<class datatype, class FN1, class FN2>
auto if_else(std::true_type,datatype&& data, FN1 &&fn1, FN2 &&) 
{return fn1(std::forward<datatype>(data));}

template<class datatype, class FN1, class FN2>
auto if_else(std::false_type,datatype&& data, FN1 &&, FN2 &&fn2)
{return fn2(std::forward<datatype>(data));}


// ---------------------------------------------------------------------



struct A{
    int id= 4;
};

struct B : A{
    int h = 900;
};

class Test{

template<class Ba>
int fn1(Ba &a){
    std::cout << "fn1 " << a.h;
    return a.h;
};

template<class A>
int fn2(A &a){
    std::cout << "fn2 " << a.id;
    a.id = 9;
    return a.id;
};



public:
    template<bool do_it>
    void go(){
        std::integral_constant<bool,do_it> do_first;

        using datatype = typename std::conditional<do_it,B,A>::type;
        datatype data;
        std::cout << std::is_same<decltype(data), A>::value;

        std::cout << if_else(do_first, data,
                    [&](auto /*B&*/ data){           // comment auto to get rid
of error

                        std::cout << std::endl;
                        std::cout << std::is_same<decltype(data), B>::value;
                        std::cout << std::endl;

                        return fn1(/*static_cast<B&>*/(data)); // static cast
not work with gcc
                    },
                    [&](A& data){ return fn2(data); }
                );                
    }
};

int main(){
    Test().template go<false>();
    Test().template go<true>();
    return 0;
}

I got the following error:

main.cpp:58:61: error: cannot call member function 'int Test::fn1(Ba&) [with Ba
= B]' without object


If change auto with B - works fine. And it compiles and work as is with clang.

P.S. Maybe somehow related to this
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49554


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2015-10-13 11:28 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-28  8:13 [Bug c++/61636] New: generic lambda "cannot call member function without object" tower120 at gmail dot com
2014-06-28  8:22 ` [Bug c++/61636] " tower120 at gmail dot com
2014-06-28  8:55 ` paolo.carlini at oracle dot com
2014-06-28 13:17 ` tower120 at gmail dot com
2014-07-06 19:58 ` abutcher at gcc dot gnu.org
2014-07-06 20:15 ` abutcher at gcc dot gnu.org
2014-07-06 20:39 ` abutcher at gcc dot gnu.org
2014-07-07  7:08 ` abutcher at gcc dot gnu.org
2014-07-07  7:15 ` abutcher at gcc dot gnu.org
2014-07-26  7:10 ` contact at jeaye dot com
2015-03-09 23:58 ` abutcher at gcc dot gnu.org
2015-03-10  0:37 ` abutcher at gcc dot gnu.org
2015-07-06 12:15 ` trippels at gcc dot gnu.org
2015-10-13 11:28 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).