在C++ 03中,表达式是rvalue或lvalue.
在C++ 11中,表达式可以是:
两类已成为五大类.
编写我编写的C++程序时遇到一些困难.
这个程序非常简单,据我所知,它符合C++标准中规定的所有规则.我已经两次阅读整个ISO/IEC 14882:2003以确定.
该计划如下:

这是我尝试使用Visual C++ 2010编译此程序时收到的输出:
c:\dev>cl /nologo helloworld.png
cl : Command line warning D9024 : unrecognized source file type 'helloworld.png', object file assumed
helloworld.png : fatal error LNK1107: invalid or corrupt file: cannot read at 0x5172
Run Code Online (Sandbox Code Playgroud)
沮丧,我尝试了g ++ 4.5.2,但同样无益:
c:\dev>g++ helloworld.png
helloworld.png: file not recognized: File format not recognized
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我认为Clang(版本3.0主干127530)必须工作,因为它的标准一致性受到高度赞扬.不幸的是,它甚至没有给我一个漂亮的,突出显示的错误消息:
c:\dev>clang++ helloworld.png
helloworld.png: file not recognized: File format not recognized
collect2: ld returned 1 exit status
clang++: error: linker (via gcc) command …Run Code Online (Sandbox Code Playgroud) 我刚刚加入了一个新的C++软件项目,我正在尝试理解这个设计.该项目经常使用未命名的命名空间.例如,类定义文件中可能会出现这样的情况:
// newusertype.cc
namespace {
const int SIZE_OF_ARRAY_X;
const int SIZE_OF_ARRAY_Y;
bool getState(userType*,otherUserType*);
}
newusertype::newusertype(...) {...
Run Code Online (Sandbox Code Playgroud)
可能导致使用未命名的命名空间的设计注意事项是什么?有哪些优点和缺点?
考虑以下程序:
struct ghost
{
// ghosts like to pretend that they don't exist
ghost* operator&() const volatile { return 0; }
};
int main()
{
ghost clyde;
ghost* clydes_address = &clyde; // darn; that's not clyde's address :'(
}
Run Code Online (Sandbox Code Playgroud)
我怎么得到clyde地址?
我正在寻找一种适用于所有类型对象的解决方案.C++ 03解决方案会很好,但我也对C++ 11解决方案感兴趣.如果可能,让我们避免任何特定于实现的行为.
我知道C++ 11的std::addressof函数模板,但我不想在这里使用它:我想了解标准库实现者如何实现这个函数模板.
What is the proper signature of the main function in C++? What is the correct return type, and what does it mean to return a value from main? What are the allowed parameter types, and what are their meanings?
这是系统特定的吗?这些规则会随着时间而改变吗?如果我违反它们会发生什么?
经过几个小时试图找到shared_ptr所在的位置后,我现在非常沮丧.我看到的所有示例都没有显示包含shared_ptr(和工作)标题的完整代码.只是陈述std,tr1并<memory>没有任何帮助!我已经下载了增强功能,但仍然没有显示!有人可以通过告诉确切的位置来帮助我吗?
谢谢你让我发泄我的挫折!
编辑:我看到我的标题已被更改.对于那个很抱歉.所以...也是因为我不清楚shared_ptr是"C++版本依赖" - >这就是为什么我没有陈述我的环境 - >因此可能为什么我很难找到它.
我正在研究MSVS2008.
编辑2:我不知道为什么,但我在包括[memory]和[boost/tr1/memory.hpp]和[boost/tr1/tr1/memory],同时寻找shared_ptr的所有地方..当然,我不能"T.
感谢所有的回复.
简短版本:在许多编程语言中返回大型对象(例如向量/数组)是很常见的.如果类有移动构造函数,这个样式现在在C++ 0x中是否可以接受,或者C++程序员认为它是奇怪/丑陋/可憎的?
长版本:在C++ 0x中,这仍然被认为是不好的形式?
std::vector<std::string> BuildLargeVector();
...
std::vector<std::string> v = BuildLargeVector();
Run Code Online (Sandbox Code Playgroud)
传统版本如下所示:
void BuildLargeVector(std::vector<std::string>& result);
...
std::vector<std::string> v;
BuildLargeVector(v);
Run Code Online (Sandbox Code Playgroud)
在较新的版本,则返回值从BuildLargeVector是一个rvalue,所以v将使用的移动构造函数来构造std::vector,假设(N)RVO不会发生.
甚至之前的C++ 0x第一形式常常是因为(N)RVO"有效"的.但是,(N)RVO由编译器决定.现在我们有rvalue引用,保证不会发生深度复制.
编辑:问题实际上与优化无关.所示的两种形式在现实世界的节目中具有几乎相同的性能.然而,在过去,第一种形式可能具有数量级更差的性能.因此,第一种形式是C++编程中的主要代码味道很长一段时间.不再了,我希望?
Visual Studio 2012的预览版(VS2010之后的下一个版本)现已推出.
有谁知道它支持哪些新的C++ 11功能?(我现在无法试一试).
我理解malloc()是如何工作的.我的问题是,我会看到这样的事情:
#define A_MEGABYTE (1024 * 1024)
char *some_memory;
size_t size_to_allocate = A_MEGABYTE;
some_memory = (char *)malloc(size_to_allocate);
sprintf(some_memory, "Hello World");
printf("%s\n", some_memory);
free(some_memory);
Run Code Online (Sandbox Code Playgroud)
为简洁起见,我省略了错误检查.我的问题是,你不能通过初始化指向内存中某些静态存储的指针来做到这一点吗?也许:
char *some_memory = "Hello World";
Run Code Online (Sandbox Code Playgroud)
在什么时候你真的需要自己分配内存而不是声明/初始化你需要保留的值?
c++ ×8
c++11 ×5
c++-faq ×2
visual-c++ ×2
boost ×1
c ×1
clang ×1
coding-style ×1
explode ×1
expression ×1
memory ×1
namespaces ×1
oop ×1
php ×1
regex ×1
shared-ptr ×1