기본 콘텐츠로 건너뛰기

bind2nd 컴파일 오류2

bind2nd 컴파일 오류

아, 시바스 리갈 같은 상황. 이럴꺼면 뭣하러 STL써!

#include <list>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;

typedef struct mystorage
{
    int i;
    char c;
} mystorage;

#if defined(POINTER_TYPE)
typedef list<mystorage*> mylist;
#else
typedef list<mystorage> mylist;
#endif

#if defined(POINTER_TYPE)
inline
bool
check(const mystorage* _s, int id)
{
    const mystorage& s(*_s);
#else
inline
bool
check(const mystorage s, int id)
// You can't use below function type.
// Standard C++ does not support ref of ref.
//check(const mystorage& s, int id)
{
#endif
    return s.i == id;
}

void
addList(mylist& lst)
{
    for ( size_t i(0); i<1000; i++ )
    {
#if defined(POINTER_TYPE)
        lst.push_front(new mystorage);
        mystorage& s(**lst.begin());
#else
        lst.push_front(mystorage());
        mystorage& s(*lst.begin());
#endif

        s.i = i;
        s.c = i%26+'A';
    }
}

int
main(int,char**)
{
    mylist lst;
    mylist::iterator ib;

    addList(lst);
    ib = find_if(lst.begin(), lst.end(),
        bind2nd(ptr_fun(check), 10)
        );

    if ( ib != lst.end() )
    {
#if defined(POINTER_TYPE)
        const mystorage& s(**ib);
#else
        const mystorage& s(*ib);
#endif
        cout << "ib->i: " << s.i << endl;
        cout << "ib->c: " << s.c << endl;
    }

    return 0;
}

Call by value하면 허천나게 느리잖아! 만약 assign operator와 copy constructor를 private으로 막아놨다면 어떻게 할꺼야! 그렇다고 뽀인따로 죄다 바꿔!?



원본 위치: http://purewell.egloos.com/3517776

* 2014-04-30: C++11 표준은 위와 같은 문제를 해결해주었다.

댓글

이 블로그의 인기 게시물

Winget 해시 무시하기

가끔씩 Winget 에서 패키지를 다운로드 했을 때, "설치 관리자 해시가 일치하지 않습니다." 오류가 뜰 때가 있다. 보안 이슈가 있지만, 그냥 무시하고 싶을 때, 아래 순서로 무시해준다. 관리자 권한 winget settings --enable InstallerHashOverride 설치 winget install --ignore-security-hash --id NirSoft.NirCmd