내 목표는 동일한 요소를 가진 다른 컨테이너를 반환하지만 변환기 멤버 함수 주소를 전달하여 다른 유형으로 변환하는 사용자 지정 컨테이너 메서드를 가져 오는 것입니다. 아래의 int main () 예제는 가장 간단한 방법으로 설명합니다.
내 문제는 (가능한 경우) 이러한 함수 매개 변수의 적절한 선언을 파악할 수 없다는 것입니다.
추신 : MSVC 컴파일러를 사용하고 있습니다.
template<typename container_type>
struct Container
{
...
template
<typename convert_type = container_type,
typename = std::enable_if<!std::is_fundamental<container_type>::value>::type,
typename = std::enable_if<!std::is_pointer<container_type>::value>::type>
Container<convert_type> apply(convert_type (const container_type::*function)() const) const
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
// note: see reference to class template instantiation 'Container<container_type>' being compiled
// error C2091: function returns function
{
Container<convert_type> result();
result.reserve(this->size());
for (const auto& i : *this)
{
result.push_back(i.*function());
}
return result;
}
}
int main()
{
Container<std::string> c = { "a", "b", "c" };
Container<const char*> a = c.apply(&std::string::c_str);
}
그것을 알아 냈습니다.
R Sahu의 발언이 정확합니다! 그러나 세 번째 실수도 있으므로 완전히 다루기 위해 다른 답변을 추가해야합니다.
1) container_type 앞에 불필요한 const가 있습니다. 이것이 "오류 C2091 : 함수가 함수를 반환"하는 이유입니다.
2) 포인터에 의한 멤버 함수 호출 구문이 잘못되었습니다.
3) 예를 들어 Container <SomeClass *>를 만들면 apply () 메서드를 사용할 수 없을뿐만 아니라 SFINAE가 충분하지 않아 해당 코드가 컴파일되지 않습니다. 함수 포인터 유형도 템플릿 인수 여야합니다. 따라서 해당 함수 템플릿의 적절한 선언은 다음과 같아야합니다.
template
<typename convert_type,
typename = std::enable_if<!std::is_fundamental<container_type>::value>::type,
typename = std::enable_if<!std::is_pointer<container_type>::value>::type,
typename func_type = convert_type (type::*)() const>
Container<convert_type> apply(func_type function) const
{
...
result.push_back((i.*function)());
...
}
이제 작동합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다