简体   繁体   English

模板函数中的默认参数值,具体取决于类型

[英]Default parameter value in template function, depending on type

Suppose I have this function to trim an std::string and decide to extend it, so that it removes not only spaces from the beginning and end, but also any character I pass in another string, like spaces, newlines and carriage returns. 假设我有这个函数来修剪std :: string并决定扩展它,这样它不仅会删除开头和结尾的空格,还会删除我在另一个字符串中传递的任何字符,如空格,换行符和回车符。

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

So basically, it would remove all characters present in In_CharsToTrim from the beginning and end of In_Original. 所以基本上,它会从In_Original的开头和结尾删除In_CharsToTrim中存在的所有字符。

Now, I had to write this for both std::string and std::wstring. 现在,我必须为std :: string和std :: wstring写这个。 Since I find that absurd, I chose to make use of templates, and it works, but I am unable to pass a default value, because the std::wstring version must get L" \\n\\r" as the default value (note that little L, there). 由于我发现荒谬,我选择使用模板,它可以工作,但我无法传递默认值,因为std :: wstring版本必须得到L" \\n\\r"作为默认值(注意那个小L,那里)。

I tried this: 我试过这个:

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

along with: 随着:

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" );

But it didn't work. 但它没有用。 It doesn't even compile. 它甚至没有编译。

Currently, what I have, is a separate function for when you call without a second parameter, but this is completely the wrong approach: 目前,我所拥有的是一个单独的函数,当你在没有第二个参数的情况下调用时,这是完全错误的方法:

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 );

Then, in this simpler Trim function, I'm simply calling the complete version. 然后,在这个简单的Trim函数中,我只是调用完整版本。

So, basically, what I'm asking is this...: 所以,基本上,我问的是这个...:

How can I pass a different default value depending on the template type? 如何根据模板类型传递不同的默认值? Basically, a specialization, in which the only change is the default parameter... 基本上是一种专业化,其中唯一的变化是默认参数......

In this case, to pass std::string ( " \\n\\r" ) in one case, and std::wstring ( L" \\n\\r" ) in the other... 在这种情况下,在一种情况下传递std::string ( " \\n\\r" )在另一种情况下传递std::string ( " \\n\\r" ) std::wstring ( L" \\n\\r" ) ...

Or... is there another way to do what I'm trying to do, here? 或者......还有另一种方法可以做我想做的事吗,在这里?

You can use a simple trait class and using its ::value as the default argument. 您可以使用简单的trait类并使用其::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";
};

Then you can do: 然后你可以这样做:

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

This is what I come up with: 这就是我提出的:

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()){
    /* ... */
}

You can also declare value as a data member instead of a function, but that would require constexpr and C++11. 您还可以将value声明为数据成员而不是函数,但这需要constexpr和C ++ 11。

In your case you might rely on specifying an initializer list of characters to do the conversion for you: 在您的情况下,您可能依赖于指定初始化字符列表来为您进行转换:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM