假设你有这个非常愚蠢的代码(这只是为了让你轻松提出即将到来的问题):
#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:我知道这是不好的实践,发现主题提到最好的方法是避免名称冲突(像这一个)....
在向您提供API时,您经常需要声明错误代码(最常见的是int),之后您经常需要提供一个转换int错误代码的函数,std::string以便能够以智能方式向用户报告错误.
我发现了一些关于如何以编程方式维护int/ std::string映射的帖子,如下所示:将错误代码映射到C++中的字符串
现在,我在想,为什么不简单地回归std::string而不是int?空字符串意味着没有错误,其他任何意味着错误+提供人类可读消息.
我们显然假设您不关心内存使用和性能(您的API通常不会调用函数,执行时间并不重要).
如果您需要客户端能够以编程方式执行某些特定操作,则可以将错误代码声明为常量.但是,你不需要任何int来std::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) 我正在将一些代码从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) 有人可以向我解释为什么这个简单的代码:
#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) 我有一段时间以前写的这个功能:
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++.
我试图制作一个类方法的地图.
即:
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
让我们考虑以下代码示例:
// 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) 想使用std::nan和std::nanf来创建带有一些自定义有效负载(非装箱)的 nan 值。
但是,它确实没有按预期工作:
但是,对于 Visual Studio 2015,该功能显然没有正确实现。cppreference.com 提出的确切样本产生:
nan("1") = nan (7ff8000000000000)
nan("2") = nan (7ff8000000000000)
Run Code Online (Sandbox Code Playgroud)
这不是我们所期望的。VS 实现有错吗?如果不是,那么用于产生7ff8000000000001和的正确参数是7ff8000000000002什么?
有没有一种方法可以使用cmake比较文件?我已经检查了https://cmake.org/cmake/help/latest/command/file.html中的所有参数
我专门QApplication为构造参数MyApplication。int
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)