c++ - Windows 与 Linux 上 std::string 的可移植性和长度
问题描述
我遇到了一些 C++11std::string
长度的可移植性问题。在 Windows 上是long long unsigned int
,但在 Linux 和 Mac 上是long unsigned int
. 我的理解是 usingauto
是解决此类问题的标准方法,但是我很难找到一种可移植的方法来通过类接口公开这些属性。
以下类在 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
字符串包装.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
s 更改为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)来避免 STL 数据类型中的类型转换std::string
(如上所示)?
解决方案
查看std::string
文档,您可以看到类型被称为
std::string::size_type
所以就用那个。您不需要知道或猜测它是 typedef的原始类型- 您已经有了一个保证正确的可用名称。
推荐阅读
- c++ - 使用霍夫曼树解码
- python - 如何在需要输出单位 L2 范数的回归问题中标准化 Keras 网络输出?
- wordpress - 我试图获得免费插件,但找不到 word press 插件?
- routing - 当部署在服务器上时,页面重新加载在 Angular 6 中找不到 404
- python-3.x - 如何正确遍历二维数组?
- python - 在 winlogon.exe 之后运行 python-script
- c# - 如何在 C# 应用程序中有多个配置文件?
- java - 添加带有凌空请求和响应的自定义标头是“数据”:“此字段是必需的”
- php - 为 API 调用创建自动动态 URL
- c# - 如果我使用 NModbus4 在其中获取浮点值,如何读取 16 位寄存器