小编Mr.*_*C64的帖子

const &&在范围内的应用?

是否有任何情况下const&&在range-for循环中使用它是否有意义?

for (const auto && x : c) // ?
Run Code Online (Sandbox Code Playgroud)

c++ for-loop const rvalue-reference c++11

14
推荐指数
1
解决办法
349
查看次数

使用const初始化constexpr:int和double的不同处理

以下代码无法在Ideone上实时编译:

#include <iostream>
using namespace std;

int main() {
    const double kPi = 3.14;
    constexpr double kPi2 = 2.0*kPi;
    cout << kPi2;
}
Run Code Online (Sandbox Code Playgroud)

错误消息是:

prog.cpp: In function 'int main()':
prog.cpp:6:30: error: the value of 'kPi' is not usable in a constant expression
  constexpr double kPi2 = 2.0*kPi;
                              ^
prog.cpp:5:15: note: 'kPi' was not declared 'constexpr'
  const double kPi = 3.14;
Run Code Online (Sandbox Code Playgroud)

const声明替换为kPiwith constexpr,它会成功编译.

在另一方面,如果int是用来代替double,好像const 还有戏剧constexpr …

c++ const constexpr c++11

14
推荐指数
1
解决办法
1310
查看次数

隐式转换在现代C++中是好还是坏?

这个提案中:

N3830 Scoped Resource - 标准库的通用RAII包装器

scoped_resource提出了RAII包装器.

在第4页,有一些代码如下:

auto hFile = std::make_scoped_resource(
      ...
    );    
...

// cast operator makes it seamless to use with other APIs needing a HANDLE
ReadFile(hFile, ...);
Run Code Online (Sandbox Code Playgroud)

Win32 API ReadFile()采用原始 HANDLE参数,而不是hFile实例scoped_resource,因此为了使上述代码有效,有一个隐式转换运算符scoped_resource.

但是,避免这种隐性转换不是"现代"建议吗?

例如,ATL/MFC CString具有隐式转换(强制转换操作符)到LPCTSTR(const char/wchar_t*即原始C字符串指针),而STL字符串具有显式 c_str()方法.

类似地,智能指针unique_ptr具有访问底层包装指针的显式 get()方法; 此博客文章中也出现了针对隐式转换的建议:

读者问答:为什么现代智能指针不会隐式转换为*?

那么,对于现代C++来说,这些隐式转换(如ATL/MFC CString和新提出的scoped_resource …

c++ implicit-conversion c++11

13
推荐指数
1
解决办法
2079
查看次数

在C++中何时使用WCHAR以及何时使用CHAR

我有个问题:

有些库使用WCHAR作为文本参数,而其他库使用CHAR(作为UTF-8):当我编写自己的库时,我需要知道何时使用WCHAR或CHAR.

c++ unicode

13
推荐指数
2
解决办法
2万
查看次数

为什么C <stdio.h> FILE*fread()比Win32 ReadFile()更快?

使用以下三种技术比较阅读文件:

  1. C <stdio.h> FILE*
  2. Win32 CreateFile()/ReadFile()
  3. Win32内存映射

我注意到#1比#2快,而#3是最快的.

例如,从最快到最慢排序,为了处理900MB测试文件,我得到了以下结果:

Win32内存映射:821.308毫秒

C文件(FILE*):1779.83 ms

Win32文件(CreateFile):3649.67毫秒

为什么C <stdio.h>技术比Win32 ReadFile()访问更快?我希望原始Win32 API的开销比CRT .我在这里错过了什么?

可编译测试C++源代码如下.


编辑

我使用4KB读取缓冲区和使用三个不同文件(具有相同内容)重复测试,以避免可能会扭曲性能测量的缓存效果,现在结果与预期一致.
例如,对于大约400 MB的文件,结果是:

  1. Win32内存映射:305.908毫秒

  2. Win32文件(CreateFile):451.402毫秒

  3. C文件(FILE*):460.579 ms


////////////////////////////////////////////////////////////////////////////////
// Test file reading using C FILE*, Win32 CreateFile and Win32 memory mapping.
////////////////////////////////////////////////////////////////////////////////


#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <exception>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <Windows.h>


//------------------------------------------------------------------------
//                      Performance (speed) measurement
//------------------------------------------------------------------------

long long counter()
{
    LARGE_INTEGER li; …
Run Code Online (Sandbox Code Playgroud)

c c++ file-io winapi

12
推荐指数
2
解决办法
7064
查看次数

隐式转换的gcc警告标志

我最近在下一个类似的上下文中遇到了一个错误:

double getSomeValue()
{
    return 4.0;
}
...
std::string str;
str = getSomeValue();
Run Code Online (Sandbox Code Playgroud)

正如您在这里看到的那样很容易发现问题,但是在一个大型代码库中getSomeValue(),调用代码不在同一个文件中,可能很难发现这doublestd::string静默转换.GCC编译这段代码很好-Wall -Wextra -Werror(这里的示例输出,我不知道使用了什么警告标志:http://ideone.com/BTXBFk).

我如何强制GCC为这些危险的隐式转换发出警告?我试过了-Wconversion,但它非常严格,它会导致大多数包含标题的错误,例如unsigned - 1.是否有较弱的版本-Wconversion

c++ gcc type-conversion gcc-warning

12
推荐指数
1
解决办法
1512
查看次数

为容器中的不同字符串类型实现编译时"static-if"逻辑

我想编写一个函数模板,对一个字符串容器进行操作,例如a std::vector.

我想支持两者CStringstd::wstring使用相同的模板功能.

问题是,CStringwstring有不同的接口,例如获取a的"长度" CString,你调用GetLength()方法,而不是你调用的wstring size()length().

如果我们在C++中有一个"静态if"功能,我可以这样写:

template <typename ContainerOfStrings>
void DoSomething(const ContainerOfStrings& strings)
{
    for (const auto & s : strings)
    {
        static_if(strings::value_type is CString) 
        {
            // Use the CString interface
        }
        static_else_if(strings::value_type is wstring)
        {   
            // Use the wstring interface
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

是否有一些模板编程技术可以使用当前可用的C++ 11/14工具实现这一目标?

PS
我知道可以DoSomething()vector<CString>和编写几个重载vector<wstring>,但这不是问题的关键.
此外,我希望这个函数模板适用于任何可以使用range-for循环进行迭代的容器.

c++ containers templates compile-time c++11

12
推荐指数
2
解决办法
4582
查看次数

为什么我不应该在这个简单的函数中使用`const`?

学习C++时,学习函数概念的第一个函数之一就是

int add(int a, int b)
{
    return a+b;
}
Run Code Online (Sandbox Code Playgroud)

现在我想知道:我应该在const这里使用-keyword,或者更确切地说不是,因此

int add(const int a, const int b)
{
    return a+b;
}
Run Code Online (Sandbox Code Playgroud)

但那会有意义吗?它会加速我的程序,做一些其他重要的事情,或只是增加混乱?

c++ const parameter-passing

12
推荐指数
3
解决办法
2614
查看次数

C++ 14在方法定义中使用auto关键字

我有几个std::unordered_maps.他们都有一个std::string关键,他们的数据不同.我想从给定地图的密钥创建一个csv字符串,因为该数据需要通过网络发送到连接的客户端.目前,我为每个地图都有一个方法.我想让这个通用,我想出了以下内容:

std::string myClass::getCollection(auto& myMap) {
    std::vector <std::string> tmpVec;
    for ( auto& elem : myMap) {
        tmpVec.push_back(elem.first);
    }
    std::stringstream ss;
    for ( auto& elem : tmpVec ) {
        ss << elem <<',';
    }
    std::string result=ss.str();
    result.pop_back(); //remove the last ','
    return result;
}
Run Code Online (Sandbox Code Playgroud)

我使用eclipse编译gcc 6.1.0和-std = c ++ 14并且它编译但它没有链接.链接器抱怨未定义的引用std::__cxx11::getCollection(someMap);

无论地图数据和我称之为的方式,它总是告诉我:

Invalid arguments ' Candidates are: std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<char>> getCollection() '

我该如何解决这个问题?

c++ parameters auto c++14

12
推荐指数
3
解决办法
922
查看次数

P/Invoke [In,Out]属性是否可选用于编组数组?

假设存在一个带有纯C接口的本机函数,如下所示,从本机DLL导出:

// NativeDll.cpp

extern "C" void __stdcall FillArray(
    int fillValue, 
    int count, 
    int* data)
{
    // Assume parameters are OK...

    // Fill the array
    for (int i = 0; i < count; i++)
    {
        data[i] = fillValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

以下P/Invoke工作正常(使用VS2010 SP1测试):

[DllImport("NativeDll.dll", CallingConvention=CallingConvention.StdCall)]
public static extern void FillArray(
    int fillValue,
    int count,
    [In, Out] int[] data
);
Run Code Online (Sandbox Code Playgroud)

以及这个P /调用,与上面相同,但没有[In, Out]属性:

[DllImport("NativeDll.dll", CallingConvention=CallingConvention.StdCall)]
public static extern void FillArray(
    int fillValue,
    int count,
    int[] data
);
Run Code Online (Sandbox Code Playgroud)

那么,这些 …

c# c++ arrays pinvoke attributes

11
推荐指数
1
解决办法
4263
查看次数