小编jpo*_*o38的帖子

名称冲突时局部变量和类属性之间的优先级

假设你有这个非常愚蠢的代码(这只是为了让你轻松提出即将到来的问题):

#include <iostream>

class A
{
public:
    A() : m(0) {}

    void showM1( int m )
    {
        std::cout << m << std::endl;
    }

    void showM2()
    {   
        int m = 5;
        std::cout << m << std::endl;
    }

    int m;
};

int main()
{
    A a;
    a.showM1( 5 );
    a.showM2();
}
Run Code Online (Sandbox Code Playgroud)

当我测试时,没有惊喜,它显示5和5.但这是否真的是确定性的?优先级是否总是赋予局部变量(或方法参数)和对象属性?

我问,因为我们在一个巨大的项目中重命名了一些变量,并且只是想确保行为不是"不确定",并且可能因平台,编译器而异.

PS:我知道这是不好的实践,发现主题提到最好的方法是避免名称冲突(像这一个)....

c++

2
推荐指数
1
解决办法
535
查看次数

将API使用std :: string作为错误代码返回是不好的做法吗?

在向您提供API时,您经常需要声明错误代码(最常见的是int),之后您经常需要提供一个转换int错误代码的函数,std::string以便能够以智能方式向用户报告错误.

我发现了一些关于如何以编程方式维护int/ std::string映射的帖子,如下所示:将错误代码映射到C++中的字符串

现在,我在想,为什么不简单地回归std::string而不是int?空字符串意味着没有错误,其他任何意味着错误+提供人类可读消息.

我们显然假设您不关心内存使用和性能(您的API通常不会调用函数,执行时间并不重要).

如果您需要客户端能够以编程方式执行某些特定操作,则可以将错误代码声明为常量.但是,你不需要任何intstd::string映射了.例如,它将是:

宣言:

static const std::string successMessage;
static const std::string fileDoesNotExistMessage;
static const std::string internalErrorMessage;

std::string openFile( const std::string& fileName );
Run Code Online (Sandbox Code Playgroud)

执行:

static const std::string successMessage = "";
static const std::string fileDoesNotExistMessage = "File does not exist";
static const std::string internalErrorMessage = "Internal error";

std::string openFile( const std::string& fileName )
{
    if ( ... ) // test …
Run Code Online (Sandbox Code Playgroud)

c++ string error-handling

2
推荐指数
1
解决办法
130
查看次数

为什么boost :: filesystem中止而不是抛出异常?

我正在将一些代码从VS2010(使用Boost 1.55)迁移到VS 2015(使用Boost 1.60)。

我最终得到“ Microsoft Visual C ++运行时库”的报告,该报告abort() has been called同时引发了异常。但是,我可以毫无问题地抛出其他异常(并且它过去曾与VS2010 / boost1.55一起使用):

#include <boost/filesystem.hpp>
#include <boost/filesystem/operations.hpp>

#include <iostream>

int main( int argc, char* argv[] )
{
    // Stepping to folder:

    try
    {
        boost::filesystem::current_path("B:/dev/msvc2015/vobs_bci/public/tst/base/cppunit/utlfile");
        std::cout << "Worked" << std::endl; // works OK
    }
    catch (...)
    {

    }

    // test throwing upon copy_directory because dource folder does not exist:

    try
    {
        boost::filesystem::copy_directory("s", "b");
    }
    catch (...)
    {
        std::cout << "Caught" << std::endl; // works OK
    }

    // test …
Run Code Online (Sandbox Code Playgroud)

c++ exception-handling boost-filesystem visual-studio-2015

2
推荐指数
1
解决办法
1916
查看次数

将元素推送到基于范围的for循环内的向量

有人可以向我解释为什么这个简单的代码:

#include <iostream>
#include <vector>

int main()
{
    std::vector<int> v = {0, 1, 2, 3, 4, 5};

    for(auto i: v)
    {
        std::cout << i << ' ';
    }

    std::cout << std::endl;

    for(auto i: v)
    {
        std::cout << i << ' ';
        v.push_back(4);
    }

    std::cout << std::endl;

    for(auto i: v)
    {
        std::cout << i << ' ';
    }
}
Run Code Online (Sandbox Code Playgroud)

代码发布在这里:http://cpp.sh/5kxdu

输出:

0 1 2 3 4 5 
0 0 2 3 4 5 
0 1 2 3 4 …
Run Code Online (Sandbox Code Playgroud)

c++ vector c++11

2
推荐指数
1
解决办法
118
查看次数

如何检查传递给函数的容器是否已排序,如果没有则对其进行排序

我有一段时间以前写的这个功能:

template <class C, class T>
static inline bool findClosestObject( const C& container, const TimeUnit& now, T& object );
Run Code Online (Sandbox Code Playgroud)
  • C 是T元素的容器
  • TimeUnit 是一个封装日期和时间的类
  • T是一个有TimeUnit信息的对象

此函数std::lower_bound在容器中执行二进制搜索(使用)以查找最接近的对象now.

当我们进行二分查找时,必须对容器进行排序.该函数用于许多种容器(C可以是std::vector,std::set,std::map...)在很多地方.有时我们使用sorted std::vector而不是std::set因为它们的内存管理更快,也因为历史问题和与使用向量的其他代码的兼容性.

问题是我在代码中找到了一个地方,开发人员findClosestObject用一个没有排序的容器调用....好的bug ...而且我无法安全地识别出可以完成的所有地方.

所以我现在需要通过在这个特定的情况下对容器进行排序来防止它(它将会很慢但是至少会工作并且保证函数返回我们希望它返回的内容)

所以我试着修改我的功能:

template <class C, class T>
static inline const C& fixOrder( const C& container, C& temp )
{
    if ( std::is_sorted( container.begin(), container.end() )
    {
        return container;
    }
    else
    { …
Run Code Online (Sandbox Code Playgroud)

c++ sorting stl vector c++11

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

映射到方法c ++

我正在使用C++.
我试图制作一个类方法的地图.
即:

map<int,void*> mapIdToMethod;
Run Code Online (Sandbox Code Playgroud)

方法:

void MyClass::MyMethod(void*);
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

mapIdToMethod.insert(make_pair(1, (void*)&MyClass::MyMethod));//Not compiled
 mapIdToMethod.insert(make_pair(1, (void*)&MyClass::MyMethod()));//Not compiled
Run Code Online (Sandbox Code Playgroud)

那么,为什么这样做最好的是什么?
谢谢!

*我无法使用 Boost

c++

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

基于getter成员函数对STL容器进行排序,而不编写额外的代码

让我们考虑以下代码示例:

// Example program
#include <iostream>
#include <string>
#include <algorithm>

class A
{
public:
    A( const std::string& name ) : name( name ) {}

    inline const std::string& getName() const { return name; }

private:
    std::string name;
};

int main()
{
    std::vector<A> v;
    v.push_back( A("b") );
    v.push_back( A("a") );

    // want to sort the container, based on it's name!
}
Run Code Online (Sandbox Code Playgroud)

我知道如何做到这一点(或者定义一个内部或外部operator<,或者声明一个functor struct/class,提供一个operator()并将它传递给std::sortfonction),例如:

bool operator<( const A& a ) const
{
    return getName() < a.getName();
} …
Run Code Online (Sandbox Code Playgroud)

c++ sorting stl

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

Visual Studio 下的 std::nan/std::nanf 有什么问题?

想使用std::nanstd::nanf来创建带有一些自定义有效负载(非装箱)的 nan 值。

但是,它确实没有按预期工作:

但是,对于 Visual Studio 2015,该功能显然没有正确实现。cppreference.com 提出的确切样本产生:

nan("1") = nan (7ff8000000000000)
nan("2") = nan (7ff8000000000000)
Run Code Online (Sandbox Code Playgroud)

这不是我们所期望的。VS 实现有错吗?如果不是,那么用于产生7ff8000000000001和的正确参数是7ff8000000000002什么?

c++ nan c++11

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

如何在CMake中比较文件

有没有一种方法可以使用cmake比较文件?我已经检查了https://cmake.org/cmake/help/latest/command/file.html中的所有参数

cmake

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

C++ 防止参数不明确 int 与 int&amp;

我专门QApplication为构造参数MyApplicationint

class MyApplication : public QApplication
{
public:
    Foo( int argc, char **argv ) : QApplication( argc, argv )
    {
        ...
    }
};
Run Code Online (Sandbox Code Playgroud)

这个类在我的代码中的许多地方再次被专门化。

不幸的是,我使用了int,而我应该使用int&(正如Qapplication预期的那样)。在 Windows 下它没有造成任何麻烦,但是当我转移到 Linux 时它开始崩溃。所以我改为MyApplication

class MyApplication : public QApplication
{
public:
    Foo( int& argc, char **argv ) : QApplication( argc, argv )
    {
        ...
    }
};
Run Code Online (Sandbox Code Playgroud)

但是,所有专门的类MyApplication也需要更新,而且它们有很多,如果不更新,代码仍然可以编译,但很可能崩溃。

我需要确定应该应用更改的所有位置,但我也想防止新代码犯同样的错误。

有没有什么方法/技巧可以防止使用int“int&”进行专门化编译?

我希望不允许使用此代码并产生编译错误:

class OtherApplication : public MyApplication 
{
public:
    Foo( …
Run Code Online (Sandbox Code Playgroud)

c++ qt

0
推荐指数
1
解决办法
76
查看次数