重载 std::string 构造函数

发布于 2025-01-05 05:43:31 字数 106 浏览 1 评论 0原文

我可以重载 std::string 构造函数吗?

我想创建一个接受 std::wstring 并返回 std::string 的构造函数。这可能吗?如何实现?

谢谢。

Can I overload the std::string constructor?

I want to create a constructor which takes std::wstring and return a std::string. is it possible and how?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兰花执着 2025-01-12 05:43:31

我可以重载 std::string 构造函数吗?

不,它需要更改 std::string 声明。

我想创建一个接受 std::wstring 并返回 std::string 的构造函数。这可能吗?如何实现?

您可以使用转换函数,例如:

std::string to_string(std::wstring const& src);

但是,您需要决定如何处理无法使用 std::string 的 8 位编码表示的符号:是否将它们转换为多字节符号或抛出异常。请参阅 wcsrtombs 函数。

Can I overload the std::string constructor?

Nope, it would require changing std::string declaration.

I want to create a constructor which takes std::wstring and return a std::string. is it possible and how?

You can have a conversion function instead, like:

std::string to_string(std::wstring const& src);

However, you need to decide what to do with symbols that can't be represented using 8-bit encoding of std::string: whether to convert them to multi-byte symbols or throw an exception. See wcsrtombs function.

旧情别恋 2025-01-12 05:43:31

而是定义一个自由函数:

std::string func(const std::wstring &)
{
}

Rather define a free function:

std::string func(const std::wstring &)
{
}
玩世 2025-01-12 05:43:31

不可以,您不能向 std::string 添加任何新的构造函数。您可以做的是创建一个独立的转换函数:

std::string wstring_to_string(const wstring& input)
{
    // Your logic to throw away data here.
}

如果您(认为您)希望这种情况自动发生,我强烈建议重新评估这个想法。您会给自己带来很多麻烦,因为在您最意想不到的时候,wstring 会自动被视为 string

No, you cannot add any new constructors to std::string. What you can do is create a standalone conversion function:

std::string wstring_to_string(const wstring& input)
{
    // Your logic to throw away data here.
}

If you (think you) want this to happen automatically, I strongly suggest re-evaluating that idea. You'll cause yourself a significant amount of headaches as wstrings are automatically treated as string when you least expect it.

小女人ら 2025-01-12 05:43:31

这不是正确的做法,我认为我在编码时吸了一些东西,但它解决了问题。检查最后一个函数“convert_str”。

#pragma once    

#include <memory>
#include <string>
#include <vector>

#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/logical.hpp>

template <typename Target, typename Source, typename cond>
struct uni_convert {
};

template <typename Target, typename Source > 
struct uni_convert<Target,Source,
    typename boost::enable_if< boost::is_same<Target, Source>, int >::type > {
    static Target doit(Source const& src) 
    {

        return src;
    }
};

template <typename Cond > 
struct uni_convert<std::string,std::wstring,
    Cond > {
    static std::string doit(std::wstring const& src) 
    {
        std::vector<char> space(src.size()*2, 0);
        wcstombs( &(*( space.begin() )), src.c_str(), src.size()*2 );
        std::string result( &(*( space.begin() )) );
        return result;
    }
};

template <typename Cond > 
struct uni_convert<std::wstring,std::string,
    Cond > {
    static std::wstring doit(std::string const& src) 
    {
        std::vector<wchar_t> space(src.size()*2, 0);
        mbstowcs( &(*( space.begin() )), src.c_str(), src.size()*2 );
        std::wstring result( &(*( space.begin() )) );
        return result;
    }
};

template< typename TargetChar >
std::basic_string< TargetChar > convert_str( std::string const& arg)
{
    typedef std::basic_string< TargetChar > result_t;
    typedef uni_convert< result_t, std::string, int > convertor_t;
    return convertor_t::doit( arg );
}

This is not the Right True Way of doing it, and I think that I had smoked something when I coded this, but it solves the problem. Check the last function, `convert_str'.

#pragma once    

#include <memory>
#include <string>
#include <vector>

#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/remove_pointer.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/logical.hpp>

template <typename Target, typename Source, typename cond>
struct uni_convert {
};

template <typename Target, typename Source > 
struct uni_convert<Target,Source,
    typename boost::enable_if< boost::is_same<Target, Source>, int >::type > {
    static Target doit(Source const& src) 
    {

        return src;
    }
};

template <typename Cond > 
struct uni_convert<std::string,std::wstring,
    Cond > {
    static std::string doit(std::wstring const& src) 
    {
        std::vector<char> space(src.size()*2, 0);
        wcstombs( &(*( space.begin() )), src.c_str(), src.size()*2 );
        std::string result( &(*( space.begin() )) );
        return result;
    }
};

template <typename Cond > 
struct uni_convert<std::wstring,std::string,
    Cond > {
    static std::wstring doit(std::string const& src) 
    {
        std::vector<wchar_t> space(src.size()*2, 0);
        mbstowcs( &(*( space.begin() )), src.c_str(), src.size()*2 );
        std::wstring result( &(*( space.begin() )) );
        return result;
    }
};

template< typename TargetChar >
std::basic_string< TargetChar > convert_str( std::string const& arg)
{
    typedef std::basic_string< TargetChar > result_t;
    typedef uni_convert< result_t, std::string, int > convertor_t;
    return convertor_t::doit( arg );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文