Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将任何内容隐式转换为字符串?_C++_String_Type Conversion_Implicit Conversion - Fatal编程技术网

C++ 如何将任何内容隐式转换为字符串?

C++ 如何将任何内容隐式转换为字符串?,c++,string,type-conversion,implicit-conversion,C++,String,Type Conversion,Implicit Conversion,我的目标是设计一个装饰std::String的String类,以提供程序所需的一些功能。我想添加的一个功能是能够隐式地将任何内容转换为我的字符串,以节省一些输入 为了实现隐式转换,我设计了以下类: std::ostream& operator<<(std::ostream& o, const String& s); class String { public: template<typename t_value> String::

我的目标是设计一个装饰std::String的String类,以提供程序所需的一些功能。我想添加的一个功能是能够隐式地将任何内容转换为我的字符串,以节省一些输入

为了实现隐式转换,我设计了以下类:

std::ostream& operator<<(std::ostream& o, const String& s);

class String {
public:
    template<typename t_value>
    String::String(t_value value) {
       std::ostringstream oss;
       oss << value;
      _str = oss.str();
    }
private:
    std::string _str;
}
并获取编译器错误,而不是构造函数中的无限循环


有简单的解决方法吗?

这个或类似的方法可以解决这个问题:

namespace HasFormattedOutput {

    namespace Detail
    {
        struct Failure{};
    }

    template<typename OutputStream, typename T>
    Detail::Failure operator << (OutputStream&, const T&);

    template<typename OutputStream, typename T>
    struct Result : std::integral_constant<
        bool,
        ! std::is_same<
            decltype((*(OutputStream*)0) << std::declval<T>()),
            Detail::Failure
        >::value
    > {};
} // namespace HasFormattedOutput

template <typename T, typename OutputStream = std::ostream>
struct has_formatted_output
:   HasFormattedOutput::Result<OutputStream, T>
{};

class X  {
    public:
    X() {}

    template <typename T>
    X(const T& t) {
        static_assert(
             has_formatted_output<T>::value, 
             "Not supported type.");
        std::ostringstream s;
        s << t;
        str = s.str();
    }

    private:
    std::string str;
};
std::ostream& operator << (std::ostream& stream, const X&) { return stream; }

struct Y  {
    Y() {}
};

int main() {
    Y y;
    X x(y);
    return 0;
}
命名空间HasFormattedOutput{
名称空间详细信息
{
结构失效{};
}
模板
细节::失败运算符{};
}//命名空间HasFormattedOutput
模板
结构具有\u格式的\u输出
:HasFormattedOutput::Result
{};
X类{
公众:
X(){}
模板
X(施工测试与测试){
静态断言(
已格式化输出::值,
“不支持的类型。”);
std::ostringstream s;

s不在周围的名称空间中声明输出运算符,只将其声明为
String
类的友元:

class String {
public:
    // This must be implemented inline here
    friend std::ostream& operator<<(std::ostream& o, const String& s) {
        return o << _str; // for example
    }

    template<typename t_value>
    String(t_value value) {
       std::ostringstream oss;
       oss << value;
      _str = oss.str();
    }
private:
    std::string _str;
};
类字符串{
公众:
//这必须在这里内联实现

friend std::ostream&Operator这与您的问题无关,我可能错了,但以下划线开头的名称不被认为是不好的做法吗?我认为这是为了避免在
to_string()中键入
to
的C++11?只使用
std::stringstream有什么错?
似乎有一些讨论,但单前导下划线至少不保留,只要它们后面没有大写字母。@AdriC.s:这样的名称就可以了,只要它们不在全局命名空间中,并且只有一个下划线,如果你感兴趣的话,讨论的是血淋淋的细节。尽管你的解决方案是有效的并且避免了无限循环,但它并没有在默认情况下增加编译器错误,它与声明<代码>运算符@ McCaly相比相当复杂,你可以考虑在例子中提到的StistaSytRead。正如我所说的,我更喜欢默认的编译器错误,而不是强制错误。
class String {
public:
    // This must be implemented inline here
    friend std::ostream& operator<<(std::ostream& o, const String& s) {
        return o << _str; // for example
    }

    template<typename t_value>
    String(t_value value) {
       std::ostringstream oss;
       oss << value;
      _str = oss.str();
    }
private:
    std::string _str;
};