Thread: std:vector:resize in gcc4.2

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    10

    std:vector:resize in gcc4.2

    hi,

    a week ago i upgraded from gcc4.0 to llvm gcc4.2. everything went well except for one thing i have really big trouble to figure out because i am more the audio signal processing kind of guy. so i really hope someone of you could help me. my issue is that the use of ::resize in stl_vector.h has completely changed.
    in gcc4.0 the following code compiled fine:
    std::vector< std::vector<myData> > mMy;

    std::for_each( mMy.begin(), mMy.end(), std::bind2nd(std::mem_fun_ref(&std::vector< myData >::resize), numMy) );


    in gcc4.2 it does not compile with error:

    no matching function for call to mem_fun_ref ..

    i am pretty sure that i do not know how to implement ::resize correctly, could someone tell me what i have to do get this fixed. i am happy if you could show me a correct code example, but hints (books) and keywords that could help me figure this out would also be greatly appreciated ..


    thanks a lot for your help ..

    kind regards,
    bzt

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It seems it has to do with the fact that mem_fun_ref expects a function with either zero or one parameters, but resize takes 2 parameters (size and default value).

    I don't know how to fix it, but I'm sure someone here does.
    Code:
    #include <functional>
    #include <vector>
    
    int main() {
      std::mem_fun_ref(&std::vector<int>::resize);
    }
    
    /* ERROR:
    
    no matching function for call to 
      mem_fun_ref( void (std::vector<int>::*)(unsigned int, int) )
    
    candidates are:
    
    template<class _Ret, class _Tp>
      std::mem_fun_ref_t<_Ret, _Tp>
        std::mem_fun_ref(_Ret (_Tp::*)()) 
    
    template<class _Ret, class _Tp>
      std::const_mem_fun_ref_t<_Ret, _Tp>
        std::mem_fun_ref(_Ret (_Tp::*)()const) 
    
    template<class _Ret, class _Tp, class _Arg>
      std::mem_fun1_ref_t<_Ret, _Tp, _Arg>
        std::mem_fun_ref(_Ret (_Tp::*)(_Arg)) 
    
    template<class _Ret, class _Tp, class _Arg>
      std::const_mem_fun1_ref_t<_Ret, _Tp, _Arg>
        std::mem_fun_ref(_Ret (_Tp::*)(_Arg)const) 
    */
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    vector<MyData>::resize actually takes two arguments, one being the new size and two being the initial value of new elements. You can't use it with mem_fun_ref because mem_fun_ref (as well as any other helper function of this concept) allows you to take a maximum of ONE argument. You should write a for loop.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should write a for loop.
    There you go! I thought there'd be a fancier solution, but sometimes you just have to write a loop.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Well, he doesn't have to write a for loop. He can still use `std::for_each' and `std::bind2nd'. He just needs to rewrite the `std::mem_fun_ref(&std::vector< myData >::resize)' bit by way of a template function that passes a default constructed object along to `std::vector<???>::resize' or use C++11 features.

    Soma

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    10
    right soma .. and i am just not getting it how ... do you have an idea ?

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    do you have an idea ?
    Yes. I do have an idea. Thanks for asking.

    Now, seriously, we are programmers here; be specific. What don't you understand?

    In the interim, why don't you write a function that represent the "meat and potatoes" of the approach whiteflags suggested.

    Eh, who am I kidding? That's not actually a suggestion. When you post again provide a function that you would use in the manually coded loop whiteflags told you to use and I'll help you adapt it.

    Soma

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    10
    sorry, i am very new to forum posting and finding solutions with others .. please excuse .. i have contacted the gnu gcc people and they gave me an answer ..

    I am fairly sure the problem is that a single resize member function
    with this signature:void resize(size_type, const T& = T());was replaced with a pair of overloaded functions with these signatures:void resize(size_type);void resize(size_type, const T&);This change is allowed by the standard, but means that&vector<myData>::resize is no longer unambiguous.You can disambiguate the expression by providing a target type toconvert to, so the compiler knows which overload you mean:typedef void (*resize_type)(std::vector< myData >::size_type);std::bind2nd(std::mem_fun_ref((resiz e_type)&std::vector< myData >::resize), numMyData) );
    if using this example i get an error that i could not cast from type void to pointer .. i do not really understand this ..

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    10
    i do understand what a cast is, but how can i tell the compiler which overload to use ?

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    10
    another approach was given me by the nice guys at stack overflow ..

    Code:
     std
    Code:
    ::for_each( mMy.begin(), mMy.end(),
    Code:
    std::bind2nd(std::mem_fun_ref(reinterpret_cast<void(std::vector<myData>::*)(std::size_t)>(&std::vector<myData>::resize)),
        numMy));
    
    this one looked really promising, but does not work for all myData types .. and throws the following error ..

    Code:
     
    // GLIBCXX_RESOLVE_LIB_DEFECTS // 402. wrong new expression in [some] allocator::construct void construct(pointer __p, const _Tp& __val) { ::new(__p) _Tp(__val); }
    


    and i have never had such an error before ..

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ...Use a lambda?
    Btw, why are you using such an old compiler?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    10
    Quote Originally Posted by Elysia View Post
    ...Use a lambda?
    Btw, why are you using such an old compiler?
    i have messed up my old systems so often with such approaches and i am using Xcode .. and i need my code to run on a system everybody else could also just buy out of the box ..

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What? I don't get you.
    "You messed up your old systems with such approaches" means...?
    "I need my code to run on a system everybody else could just buy out of the box" refers to the executable or code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2012
    Posts
    10
    if you are not that experienced with such updating gcc, i could mess up the whole system and anyway the gnu gcc people are telling me there is no need to do that ..


    I am fairly sure the problem is that a single resize member functionwith this signature:void resize(size_type, const T& = T());was replaced with a pair of overloaded functions with these signatures:void resize(size_type);void resize(size_type, const T&);This change is allowed by the standard, but means that&vector<myData>::resize is no longer unambiguous.You can disambiguate the expression by providing a target type toconvert to, so the compiler knows which overload you mean:typedef void (*resize_type)(std::vector< myData >::size_type);std::bind2nd(std::mem_fun_ref((resiz e_type)&std::vector< myData>::resize), numMyData) );i am still trying to figure out how to tell the compiler which overload to use ..

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no need and it is a good thing to do so are two very different things.
    You are kicking yourself and hurting everyone else by not updating.
    With latest GCC, you should be able to do

    Code:
    std::vector<int> test;
    std::for_each(test.begin(), test.end(), [&test](int size) { test.resize(size); });
    Now compare that to what you are trying to do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. resize image
    By lamko in forum C Programming
    Replies: 22
    Last Post: 09-01-2011, 09:59 AM
  2. Resize Listview
    By mikeman118 in forum Windows Programming
    Replies: 4
    Last Post: 06-12-2008, 07:23 PM
  3. png Image Resize
    By bhupesh.kec in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 07:52 AM
  4. std::vector resize and Class copy constructor
    By pianorain in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2005, 12:52 PM
  5. .resize
    By psyko369 in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2002, 07:51 PM