2014-07-11 1 views
4

std :: string을 잘라내어 확장하기로 결정하여 시작과 끝의 공백뿐만 아니라 전달하는 문자도 제거하도록한다고 가정합니다 공백, 줄 바꿈 및 캐리지 리턴과 같은 다른 문자열에서.템플릿 함수의 기본 매개 변수 값 (형식에 따라 다름)

std::string Trim (const std::string &In_Original, 
    const std::string &In_CharsToTrim = " \n\r"); 

기본적으로 In_CharsToTrim에있는 모든 문자는 In_Original의 시작과 끝에서 제거됩니다.

이제 std :: string 및 std :: wstring에 대해이 코드를 작성해야했습니다. 내가 터무니없는 것을 발견했기 때문에 나는 템플릿을 사용하기로 결정했다. 그러나 작동하지만 디폴트 값을 전달할 수 없다. 왜냐하면 std :: wstring 버전은 기본값으로 L" \n\r"을 가져야하기 때문이다. . 함께

template <typename Type> Type Trim (const Type &In_String, 
    const Type &In_TrimCharacters); 

:

나는이 시도

template std::string Trim (const std::string &In_String, 
    const std::string &In_TrimCharacters = " \n\r"); 
template std::wstring Trim (const std::wstring &In_String, 
    const std::wstring &In_TrimCharacters = L" \n\r"); 

그러나 그것은 작동하지 않았다. 그것은 심지어 컴파일되지 않습니다.

현재, 내가 무엇을, 두 번째 매개 변수없이 호출 할 때를위한 별도의 기능이있다, 그러나 이것은 완전히 잘못된 접근 방식이 간단 트림 기능에, 그리고

template <typename Type> Type Trim (const Type &In_String, 
    const Type &In_TrimCharacters); 
std::string Trim (const std::string &In_String); 
std::wstring Trim (const std::wstring &In_String); 

, 단순히 해요 완전한 버전을 호출.

그래서, 기본적으로, 내가 부탁 해요입니다이 ... :

가 어떻게 템플릿 유형에 따라 다른 기본 값을 전달 할 수 있습니까? 기본적으로, 유일한 변화는 기본 매개 변수 인 전문성, ...이 경우

이 ... 한 경우와 다른 std::wstring (L" \n\r")

std::string (" \n\r")을 통과하거나 ...이 내가하려는 일을하는 또 다른 방법, 여기?

답변

2

이 내가 가지고 올 것입니다 : 당신은 대신 함수의 데이터 멤버로 value를 선언 할 수

template <typename T> struct default_trim_chars; 

template<> struct default_trim_chars<std::string> { 
    static const char* value() { return " \n\r"; } 
}; 

template<> struct default_trim_chars<std::wstring> { 
    static const wchar_t* value() { return L" \n\r"; } 
}; 

template <typename Type> Type Trim (const Type &In_String, 
const Type &In_TrimCharacters = default_trim_chars<Type>::value()){ 
    /* ... */ 
} 

,하지만 constexpr 및 C++ (11)를 필요로한다.

+0

이 솔루션은 변경하지 않고 일 컴파일러 옵션을 제공합니다. 감사! –

3

간단한 특성 클래스를 사용하고 기본 인수로 ::value을 사용할 수 있습니다.

template<typename T> 
struct type_sensitive_default_argument; 

template<> 
struct type_sensitive_default_argument<std::string> 
{ 
    static constexpr char value[] = "\n\r"; 
}; 

template<> 
struct type_sensitive_default_argument<std::wstring> 
{ 
    static constexpr wchar_t value[] = L"\n\r"; 
}; 

그럼 당신은 할 수 있습니다 : 귀하의 경우 당신은 당신을 위해 변환을 할 문자의 초기화 목록 지정에 의존 할 수에서

template<class Type, 
     class Default = type_sensitive_default_argument<Type>> 
Type Trim(const Type& In_String, 
      const Type& In_TrimCharacters = Default::value); 
+0

어떤 이유로 그 코드를 컴파일 할 수 없습니다. constexpr을 인식하지 못합니다.간단히 const로 바꾸었고 클래스 선언 내에서 const 멤버 변수를 초기화한다고 불평했습니다. 그래서 초기화를 제거하고 그것을 선언 밖으로 놓고 벽돌 벽에 부딪 쳤습니다. 내가이 : 오류 : 기본 템플릿 인수를 사용하지 않을 수 있습니다 함수 템플릿 -std = C++ 11 또는 -std = gnu ++ 11 –

+0

나는 위의 모든 오류가 나는 C99 .. 아직도 사용하고 있기 때문에 것 같아요. 버머 야. 컴파일러 설정을 변경하는 것이 가능한지 알아보기 위해 차이점을 확인하고 테스트 해 보겠습니다. –

1

을 :

template <typename Type> Type Trim (const Type &In_String, 
    const Type &In_TrimCharacters = { ' ', '\r', '\n' }) 
{ 
    // ... 
} 
관련 문제