2012-10-13 3 views
1

const 함수 참조 (템플릿 매개 변수)를 원한다는 것을 어떻게 선언합니까? 예 :const 템플릿 매개 변수의 함수에 대한 const 참조?

template< bool (&func)(int arg) > 
void foo(int stuff); 

하지만 const? 나는 icpc에 다음과 같은 컴파일하려고하면 구체적으로

:

template<bool (&func)(int arg)> 
bool id(int arg) { 
    return func(arg); 
} 

class Foo { 
public: 
    Foo() {}; 
    virtual ~Foo() {}; 
    bool bar(int arg) { return true; } 
    bool bar2(int arg) { 
    return id<bar>(arg); 
    }; 
}; 

int main() { 
    return 0; 
} 

내가 g++로,

$ icpc foo.cpp 
foo.cpp(12): error: no instance of function template "id" matches the argument list 
      argument types are: (int) 
     return id<bar>(arg); 
      ^
compilation aborted for foo.cpp (code 2) 

를 얻거나, 내가 할

$ g++ foo.cpp 
foo.cpp: In member function ‘bool Foo::bar2(int)’: 
foo.cpp:13:23: error: no matching function for call to ‘id(int&)’ 
    return id<bar>(arg); 
        ^
foo.cpp:13:23: note: candidate is: 
foo.cpp:2:6: note: template<bool (& func)(int)> bool id(int) 
bool id(int arg) { 
    ^
foo.cpp:2:6: note: template argument deduction/substitution failed: 
foo.cpp:13:23: error: could not convert template argument ‘Foo::bar’ to ‘bool (&)(int)’ 
    return id<bar>(arg); 
        ^

그러나 경우 대신

에서와 같이 바를 최상위로 이동합니다.210
template<bool (&func)(int arg)> 
bool id(int arg) { 
    return func(arg); 
} 

bool bar(int arg) { return true; } 

class Foo { 
public: 
    Foo() {}; 
    virtual ~Foo() {}; 
    bool bar2(int arg) { 
    return id<bar>(arg); 
    }; 
}; 

int main() { 
    return 0; 
} 

잘 컴파일됩니다. 왜 이런 일이 일어나고, 바를 글로벌화하지 않고 어떻게 해결할 수 있습니까?

참고 : (icpc와)

CollisionWorld.cpp(73): error: a reference of type "bool (&)(const Line &, vec_dimension={double}, vec_dimension={double}, vec_dimension={double}, vec_dimension={double})" (not const-qualified) cannot be initialized with a value of type "bool (const Line &, vec_dimension={double}, vec_dimension={double}, vec_dimension={double}, vec_dimension={double})" 
    QuadTree<Line, vec_dimension, line_inside_box_with_time> *quad_tree = 
           ^

(g++와)

: 내 원래의 코드에서, 나는 오류 "(안 const를 수식은) 유형의 값으로 초기화 할 수 없습니다"얻고 있었다
CollisionWorld.cpp:73:58: error: could not convert template argument ‘CollisionWorld::line_inside_box_with_time’ to ‘bool (&)(const Line&, double, double, double, double)’ 
    QuadTree<Line, vec_dimension, line_inside_box_with_time> *quad_tree = 
                 ^
+0

const와는 아무런 관련이 없습니다. 멤버 함수는 함수가 아닙니다! –

+0

@KerrekSB 멤버 함수는 함수입니다. 멤버 함수를 호출하지 않고 멤버 함수의 이름을 지정하면 함수 유형의 rvalue 표현식이됩니다. 아마도 그들은 좋은 진단을 제공하려고 시도하고 이니셜 라이저가 rvalue인지 여부를 확인했지만 멤버 함수 초기화 프로그램을 잊어 버렸을 것입니다. –

+0

@ JohannesSchaub-litb : 함수를 호출 할 수는 있지만 멤버 함수는 호출 할 수 없습니다. 당신은 * 객체 *가 필요합니다. 그것은 많은 사람들이 작은 콜백을 시도 할 때 혼란 스러울 정도로 사람들이 실제로 어떤 기능을하는지 생각하게하는 희망에서이 급진적 성명서를 시험해보고 싶습니다. –

답변

1

문제는 템플릿에 멤버 함수가 아닌 자유 함수가 있어야한다는 것입니다. 당신이 바 (넣을 때 푸에서) 작동 이유

이처럼 시도이며, 다음 this 포인터와 기능 :

template<typename C, bool (C::*func)(int arg)> 
bool id(C *mthis, int arg) { 
    return (mthis->*func)(arg); 
} 

class Foo { 
public: 
    Foo() {}; 
    virtual ~Foo() {}; 
    bool bar(int arg) { return true; } 
    bool bar2(int arg) { 
    return id<Foo, &Foo::bar>(this, arg); 
    }; 
}; 
+0

http : //stackoverflow.com/questions/4387971/c-passing-method-pointer-as-template-argument가 관련되어 있습니다. – perh

0

이 멤버 함수를 호출하려면 두 가지가 필요합니다. 그러므로 당신이 쓰는 것처럼 쉬운 일은 아닙니다. id에는 this 포인터가 필요합니다!

template<bool (Foo::*func)(int)> 

을하지만 여전히, 당신은 함수와 멤버 함수에 모두 작동하는 진정한 id 기능을 구현할 수 없습니다 :

템플릿 정의는 다음과 같을 것이다.

관련 문제