CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    A 'customised' std::list ??

    I'm building some code which uses a library called lua. One of its header files contains a template class which (I think) is some kind of customised std::list - i.e. it's a cutdown version which only includes certain functions from a regular std::list. Here's what it looks like in the header file:-

    Code:
      template <class T>
      Class<std::list<T> > beginStdList (char const* name)
      {
        typedef std::list<T> LT;
        return beginConstStdList<T> (name)
          .addFunction("clear", (void (LT::*)()) & LT::clear)
          .addFunction("pop_front", (void (LT::*)()) & LT::pop_front)
          .addFunction("unique", (void (LT::*)()) & LT::unique) // <--- VC2019 COMPILER ERROR HERE !!
          .addFunction("push_front", (void (LT::*)(const T&)) & LT::push_front)
          .addFunction ("push_back", (void (LT::*)(const T&))&LT::push_back);
      }
    The code built fine with VC2008 - but VC2019 objects to the "unique" line which gives this compiler error if anything #incudes that header file:-


    Code:
    error C2440: 'type cast': cannot convert from 'overloaded-function' to 'void (__cdecl std::list<std::string,std::allocator<std::string>>::* )(void)'
    F:\libs\lua\LuaBridge\detail\Namespace.h(1905,24): message : None of the functions with this name in scope match the target type
    FWIW Namespace.h(1905,24) refers to the section beginning with void (LT::*)() - i.e. at the line that's failing. Does this make sense to anyone here? The function signature seem to be right AFAICT - i.e. std::list::unique() is a function which takes no parameters and returns void



    [Edit...] In case it helps, I've just found a few others which give me the same compiler error:-

    Code:
    .addFunction("front", (void (LT::*)()) & LT::front)
    .addFunction("back", (void (LT::*)()) & LT::back)
    .addFunction("end", (void (LT::*)()) & LT::end)
    Can anyone think of something which those particular functions have in common but which isn't shared by the ones that work - sort() / empty() / clear() / reverse() etc ??
    Last edited by John E; March 4th, 2021 at 06:17 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: A 'customised' std::list ??

    I think that this is due to changes in the stl implementation that means that some stl class members are no longer 'true functions' ie functions to which an address can be taken. There's nothing in the standard which means they have to be (?).

    If this is the case, then I don't know of any work-around.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: A 'customised' std::list ??

    Thanks 2kaud - but isn't that true for all template functions? I've never quite got my head around templates but my understanding is that they're not "real" functions and classes - they're more like a "set of instructions" for building funcs & classes based around a particular object type.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: A 'customised' std::list ??

    My response in my post #2 is based upon what I remember reading some time ago (MSDN blog post?) about some changes to the way Microsoft implement some class member functions which would have some implications like what you're seeing now. I could be wrong, of course, regarding my recollection.

    Can you produce a minimal 'compilable' example that shows the problem without using 3rd party code? - what's .addFuncton() ?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: A 'customised' std::list ??

    What version of lua are you using? The latest version is 5.4.2

    There's also info on the internet as to how to use lua with C++ and classes as lua seems to be written in C. I've never used it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: A 'customised' std::list ??

    It looks like we're using ver 5.3.6 (about 2 x versions ago). I'll be busy tomorrow but if I can find some time at the weekend I'll try to product that minimal sample.

    My gut feeling is that this is somehow connected to those overloaded functions but in the meantime... I can't find an implementation for .addFunction() (i.e. with a leading period) but there are 2 x functions called addFuntion(). One is for handling free functions:-

    Code:
      //----------------------------------------------------------------------------
      /**
          Add or replace a free function.
      */
      template <class FP>
      Namespace& addFunction (char const* name, FP const fp)
      {
        FUNDOC ("Free Function", name, FP)
        assert (lua_istable (L, -1));
    
        new (lua_newuserdata (L, sizeof (fp))) FP (fp);
        lua_pushcclosure (L, &CFunc::Call <FP>::f, 1);
        rawsetfield (L, -2, name);
    
        return *this;
      }
    and the other (I'm guessing the one that gets called in this case) is for member functions:-

    Code:
        //--------------------------------------------------------------------------
        /**
            Add or replace a member function.
        */
        template <class MemFn>
        Class <T>& addFunction (char const* name, MemFn mf)
        {
          FUNDOC("Member Function", name, MemFn)
          CFunc::CallMemberFunctionHelper <MemFn, FuncTraits <MemFn>::isConstMemberFunction>::add (L, name, mf);
          return *this;
        }
    Unfortunately - CallMemberfunctionHelper() just calls other lua functions which call other lua functions etc, etc (so it probably won't be easy to produce a simple example!!)
    Last edited by John E; March 4th, 2021 at 10:13 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: A 'customised' std::list ??

    Quote Originally Posted by John E View Post
    My gut feeling is that this is somehow connected to those overloaded functions
    I don't know if this'll hep or just complicate things but this seems to be the current implementation for std::list::unique() (VS2019)

    Code:
    	void unique()
    		{	// erase each element matching previous
    		_Unique(equal_to<value_type>());
    		}
    
    	void unique(_Valcomp_dt^ _Pred)
    		{	// erase each element satisfying _Pred with previous
    		_Unique(_Pred);
    		}
    
    	template<typename _Pr2_t>
    		void unique(_Pr2_t _Pred)
    		{	// erase each element satisfying _Pred with previous
    		_Unique(_Pred);
    		}
    
    	template<typename _Pr2_t>
    		void _Unique(_Pr2_t _Pred)
    		{	// erase each element satisfying _Pred with previous
    
    			// The implementation stuff
    		}
    and here's the previous one from VS2005:-

    Code:
    	void unique()
    		{	// erase each element matching previous
    
    			// The implementation stuff
    		}
    
    	template<class _Pr2>
    		void unique(_Pr2 _Pred)
    		{	// erase each element satisfying _Pred with previous
    
    			// The implementation stuff
    		}
    Last edited by John E; March 4th, 2021 at 10:52 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: A 'customised' std::list ??

    std::list::unique() is a function which takes no parameters and returns void
    That's probably the issue. All the lines which now don't compile are those that reference functions that no longer return void, but have a return value - as of C++20! eg for C++20, unique() returns a type size_type which is the number of elements removed.

    ie for those functions, the type specified:

    Code:
    void (LT::*)()
    is no longer correct - hence the errors.

    Are you compiling as language standard preview?
    Last edited by 2kaud; March 4th, 2021 at 10:56 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  9. #9
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: A 'customised' std::list ??

    Quote Originally Posted by 2kaud View Post
    Are you compiling as language standard preview?
    Sorry - I'm not quite sure what that means - but I think this might be significant...

    Quote Originally Posted by John E View Post
    In case it helps, I've just found a few others which give me the same compiler error:-

    Code:
    .addFunction("front", (void (LT::*)()) & LT::front)
    .addFunction("back", (void (LT::*)()) & LT::back)
    .addFunction("end", (void (LT::*)()) & LT::end)
    Can anyone think of something which those particular functions have in common but which isn't shared by the ones that work - sort() / empty() / clear() / reverse() etc ??
    It looks like all the functions which fail are ones that offer an overloaded version - e.g.

    reference back();
    const_reference back() const;

    iterator end();
    const_iterator end() const;

    void unique()
    void unique(_Valcomp_dt^ _Pred)


    [Edit... I just tried changing the call to unique() to specify the one that tales no arguments:- i.e. .addPtrFunction ("unique", (void (LT::*)())&LT::unique()) got changed to .addPtrFunction ("unique", (void (LT::*)())&LT::unique(void))

    This now gives me a different error:-

    Code:
    F:\libs\lua\LuaBridge\detail\Namespace.h(1980,72): error C2143: syntax error: missing ')' before '<tag>::*'
    Last edited by John E; March 4th, 2021 at 11:33 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: A 'customised' std::list ??

    Sorry - I'm not quite sure what that means - but I think this might be significant...
    In VS2019, for the project/properties/general/C++ Language standard

    What's the value? Preview means using C++20 which has return values for these functions. C++17 and previous had return void for these functions.

    Code:
    .addPtrFunction ("unique", (void (LT::*)())&LT::unique())
    does specify no argument and returning void. I don't think it's the overloading that's the issue - but the return value which now isn't void.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  11. #11
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: A 'customised' std::list ??

    Okay, got it. It's currently set to Default (ISO C++14 Standard)

    Quote Originally Posted by 2kaud View Post
    I don't think it's the overloading that's the issue - but the return value which now isn't void.
    Maybe - but the functions with overloads are always the ones which give the compiler error
    Last edited by John E; March 4th, 2021 at 01:02 PM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: A 'customised' std::list ??

    It's currently set to Default (ISO C++14 Standard)
    I'd change it to C++17 - and re-compile everything.

    This C++17 example code demonstrates the problem:

    Code:
    #include <list>
    
    using LT = std::list<int>;
    using func_t = void(LT::*)();
    
    int main()
    {
    	func_t f1 = &LT::clear;
    	func_t f2 = &LT::pop_front;
    	func_t f3 = &LT::unique;	// BAD
    }
    Looking at the implementation of list::unique for my VS2019:

    Code:
    auto unique() { // erase each element matching previous
            return unique(equal_to<>{});
    }
    The return type is auto. Looking at the implementations of those that work and those that don't - all of those that work have a return of void (for C++17) and those that don't work don't have a return of void.

    I believe this is the problem - and I don't know of any answer.

    PS. I've also tried:

    Code:
    auto f3 = &LT::unique;
    which gives the error "error C3535: cannot deduce type for 'auto' from 'overloaded-function'"

    If the compiler can't deduce the return type........
    Last edited by 2kaud; March 5th, 2021 at 06:14 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  13. #13
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: A 'customised' std::list ??

    Many thanks 2kaud - I just found a few minutes to try this....

    I've tried ISO C++14 Standard (/std:c++14) and also ISO C++17 Standard (/std:c++17) and Preview - Features from the Latest C++ Working Draft (/std:c++latest)

    What I'm seeing here is that all 3 of them give me:- error C2440: 'initializing': cannot convert from 'overloaded-function' to 'func_t'

    In fact, it's still looking like a problem with overloads....

    Code:
    IGNORE THIS CODE !!
    #include <list>
    
    using LT = std::list<int>;
    using func_t = void(LT::*)();
    
    int main (int argc, char *argv[])
    {
    // Non-overloaded members
      func_t f1 = &LT::reverse;
      func_t f2 = &LT::clear;
      func_t f3 = &LT::sort;
      func_t f4 = &LT::pop_front;
    
    // Members with overloads
      func_t f5 = &LT::unique;    // BAD
      func_t f6 = &LT::front;     // BAD
      func_t f7 = &LT::end;       // BAD
    
      return 0;
    }
    My bad - I realised this morning that front() and end() don't return type void - here's the modified code...

    Code:
    #include <list>
    
    using LT = std::list<int>;
    using func_t = void(LT::*)();
    
    using func_ref_t = LT::reference(LT::*)();
    using func_iter_t = LT::iterator(LT::*)();
    
    int main (int argc, char *argv[])
    {
    // Non-overloaded members
      func_t f1 = &LT::reverse;     // Good in MSVC
      func_t f2 = &LT::clear;       // Good in MSVC
      func_t f3 = &LT::sort;        // Good in MSVC
      func_t f4 = &LT::pop_front;   // Good in MSVC
    
    // Members with overloads
      func_t f5 = &LT::unique;      // BAD (error C2440)
      func_ref_t f6  = &LT::front;  // Now good
      func_iter_t f7 = &LT::end;    // Now good
    
      return 0;
    }
    So it looks like this is specifically a problem with unique()

    BTW - why did this get moved to Non-Visual C++ issues? AFAIK it's specifically a problem with Visual C++
    Last edited by John E; March 6th, 2021 at 02:38 AM.
    "A problem well stated is a problem half solved. - Charles F. Kettering

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,562

    Re: A 'customised' std::list ??

    The code compiles OK with both gcc and clang as C++17 or as C++20 with a change for unique. See https://wandbox.org/permlink/tfhEOLLXuzGTdh7J

    The issue is only with VS. Hence I've moved this to 'VC Bugs & Fixes'

    I've also raised a bug report with the VS team. I'll report back any updates here.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  15. #15
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,780

    Re: A 'customised' std::list ??

    Thanks 2kaud, I do appreciate all your efforts with this! Unfortunately CodeGuru won't let me give you an extra rating but you definitely deserve one !!
    "A problem well stated is a problem half solved. - Charles F. Kettering

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured