2017-12-16 29 views
2

const_cast를 이미 구현 된 메서드의 비 const 버전을 만드는 데 사용할 수 있습니까? 이 줄을 따라 (실제 작업을 수행하는 const 메서드에 대한 권장 사항을 통해) 뭔가를 본 것 같지만 실제로 작동하는 방법을 잘 모르겠습니다.const_cast를 사용하여 메서드의 비 const 변형 만들기

Value& const search(key) const { 
    // find value with key 
    return value; 
} 

Value& search(key) { 
    return const_cast<Value&>(search(key)); 
} 

이렇게하지 않으면 코드가 중복되지 않은 비 const 함수를 만드는 데 권장되는 방법은 무엇입니까?

+1

먼저 "중복성"이라는 단어를 찾아보십시오. 그것은 "복제"를 의미하지 않습니다. 기다려. 알았어. const를 캐스팅하는 것은 당신이 나쁜 처지에서 그리고 마감 시간까지 자신을 발견했을 때만하는 일입니다. 예외가 있습니다. 예를 들어, 클래스가 공용에 대해 투명한 캐시를 가지고있는 경우. const가 언어에 도입되었을 때, "const"가 정말로, 실제로는 상수인지, 아니면 "마치"상수인지에 대한 논쟁이있었습니다. "것처럼"이겼고, 따라서 const를 던지면서 const가 탄생했습니다. –

+0

관련 : https://stackoverflow.com/q/123758/1896169 – Justin

답변

3

그것을 할 수있는 가장 쉬운 방법은 C++ 17에서 as_const 함께 : 그것없이

Value& search(key) { 
    return const_cast<Value&>(std::as_const(*this).search(key)); 
} 

이 대신이 작업을 수행 할 수 있습니다 (직접 또는 그것을 구현, 매우 어렵지 않다)

Value& search(key) { 
    return const_cast<Value&>(static_cast<const T&>(*this).search(key)); 
} 

여기서 T은 클래스의 유형입니다 (decltype으로 일반 솔루션을 사용할 수 있지만 decltype(*this)이 참조 유형이므로 실제로보기가 어렵습니다).

as_const 구현 here 또는 일반 캐스트 here을 살펴볼 수 있습니다.

+0

C++ 14와 호환되는 다른 방법이 있습니까? – Davar

+0

@Davar이 (가) 업데이트되었습니다. – user975989

+0

'this'를 상응하는'const' 포인터에 던지거나'as_const()'의 자체 버전을 정의 할 수 있습니다, 예 :'template T const & as_const (T const & x) {return x; }'. 개인적으로 나는'const'를 캐스팅하는 것을 싫어하고'static'이나'friend' 함수를 사용하여 잠재적으로 templaize 알고리즘을 구현하고 그 결과를'const'/non-'const' 과부하로 반환합니다. –

1

두 가지 접근법.

첫째 :

namespace notstd{ // backported C++17 
    template<class T> 
    T const& as_const(T& t){return t;} 
    template<class T> 
    T const&& as_const(T&& t){return t;}  
} 
namespace utility { // not ever in std 
    template<class T> 
    T& remove_const(T const& t){return const_cast<T&>(t);} 
    template<class T> 
    T&& remove_const(T const&& t){return const_cast<T&&>(t);} 
} 

다음 : 또는

Value& const search(Key key) const { 
    // find value with key 
    return value; 
} 

Value& search(Key key) { 
    return utility::remove_const(notstd::as_const(*this).search(key)); 
} 

나 : 우리는 self 어쩌면 const가있는 친구 템플릿에 작업을 위임

Value& const search(Key key) const { 
    return search(*this, key); 
} 

Value& search(Key key) { 
    return search(*this, key); 
} 
private: 
    template<class Self> 
    friend decltype(auto) search(Self& self, Key key){ 
    // find value with key 
    } 

.