std::to_string - more than instance of overloaded function matches the argument list

后端 未结 2 940
南笙
南笙 2020-11-27 16:36

counter is an int

void SentryManager::add(std::string name,std::shared_ptr){
    name = name + std::to_string(counter         


        
相关标签:
2条回答
  • 2020-11-27 17:28

    You have tripped over C++ DR 1261, which reads in part

    The code "int i; to_string(i);" fails to compile, as 'int' is ambiguous between 'long long' and 'long long unsigned'. It seems unreasonable to expect users to cast numbers up to a larger type just to use to_string.

    The proposed resolution is to add more overloads. GCC has implemented this already; I guess MSVC hasn't.

    0 讨论(0)
  • 2020-11-27 17:32

    In VC++ 2010 there are three overloads of std::to_string that take long long, unsigned long long, and long double, respectively – clearly int is none of these, and no one conversion is better than another (demo), so the conversion cannot be done implicitly/unambiguously.

    In terms of real C++11 support, this is a failing on the part of VC++ 2010's standard library implementation – the C++11 standard itself actually calls for nine overloads of std::to_string ([string.conversions]/7):

    string to_string(int val);
    string to_string(unsigned val);
    string to_string(long val);
    string to_string(unsigned long val);
    string to_string(long long val);
    string to_string(unsigned long long val);
    string to_string(float val);
    string to_string(double val);
    string to_string(long double val);
    

    Had all of these overloads been present, you obviously wouldn't have this problem; however, VC++ 2010 wasn't based on the actual C++11 standard (which did not yet exist at the time of its release), but rather on N3000 (from 2009), which does not call for these additional overloads. Consequently, it's harsh to blame VC++ too much here...

    In any case, for only a handful of calls, there's nothing wrong with using a cast to resolve the ambiguity yourself:

    void SentryManager::add(std::string& name, std::shared_ptr<Sentry>) {
        name += std::to_string(static_cast<long long>(counter));
    }
    

    Or, if there's heavy usage of std::to_string in your codebase, write a few wrappers and use those instead – this way, no call-site casting is needed:

    #include <type_traits>
    #include <string>
    
    template<typename T>
    inline
    typename std::enable_if<std::is_integral<T>::value && std::is_signed<T>::value, std::string>::type
    to_string(T const val) {
        return std::to_string(static_cast<long long>(val));
    }
    
    template<typename T>
    inline
    typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, std::string>::type
    to_string(T const val) {
        return std::to_string(static_cast<unsigned long long>(val));
    }
    
    template<typename T>
    inline typename std::enable_if<std::is_floating_point<T>::value, std::string>::type
    to_string(T const val) {
        return std::to_string(static_cast<long double>(val));
    }
    
    // ...
    
    void SentryManager::add(std::string& name, std::shared_ptr<Sentry>) {
        name += to_string(counter);
    }
    

    I can't check whether VC++ 2010 succeeds or fails with the above usage of SFINAE; if it fails, the following – using tag dispatch instead of SFINAE – should be compilable (if potentially less clear):

    #include <type_traits>
    #include <string>
    
    namespace detail {
        template<typename T>                   // is_float         is_unsigned
        inline std::string to_string(T const val, std::false_type, std::false_type) {
            return std::to_string(static_cast<long long>(val));
        }
    
        template<typename T>                   // is_float         is_unsigned
        inline std::string to_string(T const val, std::false_type, std::true_type) {
            return std::to_string(static_cast<unsigned long long>(val));
        }
    
        template<typename T, typename _>       // is_float
        inline std::string to_string(T const val, std::true_type, _) {
            return std::to_string(static_cast<long double>(val));
        }
    }
    
    template<typename T>
    inline std::string to_string(T const val) {
        return detail::to_string(val, std::is_floating_point<T>(), std::is_unsigned<T>());
    }
    
    0 讨论(0)
提交回复
热议问题