c++ - Windows 与 Linux 上 std::string 的可移植性和长度

标签 c++ c++11 stl portability stdstring

我遇到了 C++11 std::string 长度的一些可移植性问题。在 Windows 上它是 long long unsigned int 但在 Linux 和 Mac 上它是 long unsigned int。我的理解是,使用 auto 是解决此类问题的标准方法,但我很难找到一种可移植的方法来通过类接口(interface)公开这些属性。


以下类在 Linux GCC 7.3.0(以及 MacOS)上编译和运行没有问题:

g++ -g -O2 -std=c++11 -Werror=conversion stringwrap.cc
./a.out
3

但是在 Windows (g++ 8.1.0 MinGW-W64 x86_64-posix-seh-rev0) 上,我得到以下编译错误:

C:\temp\v0.11>g++ -g -O2 -std=c++11 -Werror=conversion stringwrap.cc
In file included from stringwrap.cc:1:
stringwrap.h: In function 'long unsigned int determineFirstPosition(std::__cxx11::string)':
stringwrap.h:35:20: error: conversion from 'std::__cxx11::basic_string<char>::size_type' {aka 'long long unsigned int'}
to 'long unsigned int' may change value [-Werror=conversion]
     return s.length();
            ~~~~~~~~^~
stringwrap.cc: In member function 'long unsigned int Stringwrap::length() const':
stringwrap.cc:9:23: error: conversion from 'std::__cxx11::basic_string<char>::size_type' {aka 'long long unsigned int'}
to 'long unsigned int' may change value [-Werror=conversion]
     return str_.length();
            ~~~~~~~~~~~^~
cc1plus.exe: some warnings being treated as errors

stringwrap.h

#include <iostream>
#include <string>

class Stringwrap
{
  private:
    std::string str_;

  public:

    Stringwrap(const std::string& str);

    unsigned long int length() const;

    unsigned long int getFirstPosition() const;
};

inline unsigned long int determineFirstPosition(const std::string s)
{
    for (unsigned long int i = 0; i < s.length(); ++i)
    {
        switch (s.at(i))
        {
            case ' ':
            {
                break;
            }
            default:
            {
                return i;
            }
        }
    }

    return s.length();
}

stringwrap.cc

#include "stringwrap.h"

Stringwrap::Stringwrap(const std::string& str) : str_(str)
{
}

unsigned long int Stringwrap::length() const
{
    return str_.length();
}

unsigned long int Stringwrap::getFirstPosition() const
{
    return determineFirstPosition(str_);
}

int main()
{
    Stringwrap sw = *new Stringwrap("   x   ");
    std::cout << sw.getFirstPosition() << std::endl;
}

我已经尝试将所有 unsigned long int 更改为 auto,并且使用 -std=c++11 我得到以下错误:

C:\temp\v0.11>g++ -g -O2 -std=c++11 -Werror=conversion stringwrap.cc
In file included from stringwrap.cc:1:
stringwrap.h:13:19: error: 'length' function uses 'auto' type specifier without trailing return type
     auto length() const;
                   ^~~~~
stringwrap.h:13:19: note: deduced return type only available with -std=c++14 or -std=gnu++14
stringwrap.h:15:29: error: 'getFirstPosition' function uses 'auto' type specifier without trailing return type
     auto getFirstPosition() const;
                             ^~~~~
stringwrap.h:15:29: note: deduced return type only available with -std=c++14 or -std=gnu++14
stringwrap.h:18:55: error: 'determineFirstPosition' function uses 'auto' type specifier without trailing return type
 inline auto determineFirstPosition(const std::string s)
                                                       ^
stringwrap.h:18:55: note: deduced return type only available with -std=c++14 or -std=gnu++14
stringwrap.h: In function 'auto determineFirstPosition(std::__cxx11::string)':
stringwrap.h:35:21: error: inconsistent deduction for auto return type: 'int' and then 'long long unsigned int'
     return s.length();
                     ^
stringwrap.cc: At global scope:
stringwrap.cc:7:27: error: 'length' function uses 'auto' type specifier without trailing return type
 auto Stringwrap::length() const
                           ^~~~~
stringwrap.cc:7:27: note: deduced return type only available with -std=c++14 or -std=gnu++14
stringwrap.cc:12:37: error: 'getFirstPosition' function uses 'auto' type specifier without trailing return type
 auto Stringwrap::getFirstPosition() const
                                     ^~~~~
stringwrap.cc:12:37: note: deduced return type only available with -std=c++14 or -std=gnu++14

当我使用 auto 并编译 --std=c++14 时,出现以下错误:

C:\temp\v0.11>g++ -g -O2 -std=c++14 -Werror=conversion stringwrap.cc
In file included from stringwrap.cc:1:
stringwrap.h: In function 'auto determineFirstPosition(std::__cxx11::string)':
stringwrap.h:35:21: error: inconsistent deduction for auto return type: 'int' and then 'long long unsigned int'
     return s.length();
                     ^

问题:我如何编写可移植的 C++11 代码(Linux、Windows)来避免像 std::string 这样的 STL 数据类型中的类型转换(如所示以上)?

最佳答案

查看 std::string文档,你可以看到这个类型叫做

std::string::size_type

所以就用那个吧。您无需知道或猜测它是 的 typedef 是什么原始类型 - 您已经有了一个保证正确的可用名称。

关于c++ - Windows 与 Linux 上 std::string 的可移植性和长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53395683/

相关文章:

c++ - 为什么 `clang-format`和 `git clang-format`的结果不同

c++ - std::bind 和 std::thread 总是复制参数背后的基本原理是什么?

c++ - 在基于范围的 for 循环中使用 shared_ptr 到 std::vector

c++ - 复制的 std::list 中的垃圾

c++ - 如果在 VC 编译器中包含诸如 std::map 之类的容器,类的内存布局是什么

c++ - 事件对象和条件变量的区别

c++ - 如何访问 std::list 的第一个元素?

c++ - 如何使用包装在 ComPtr 中的 Direct3D 11 指针来获取 11.1 接口(interface)?

c++ - std::generate_n 无法生成我的对象

c++ - 使用 std::thread 调用重载的成员函数