Function that accepts both lvalue and rvalue arguments

Is there a way to write a function in C++ that accepts both lvalue and rvalue arguments, without making it a template? For example, suppose I write a function print_stream that reads from an istream and prints the data that was read to the screen, or something. I think it's reasonable to call print_stream like this: fstream file{"filename"}; print_stream(file); as well as like this: pr

接受左值和右值参数的函数

有没有办法在C ++中编写一个接受左值和右值参数的函数,而不必将其作为模板? 例如,假设我编写了一个函数print_stream ,它从istream中读取并打印读取到屏幕上的数据,或者其他东西。 我认为这样调用print_stream是合理的: fstream file{"filename"}; print_stream(file); 以及像这样: print_stream(fstream{"filename"}); 但是,我该如何声明print_stream以使两个使用都有效? 如果我声明为 void print_stream(ist

Exact difference between rvalue and lvalue

While I was reading http://thbecker.net/articles/rvalue_references/section_01.html, I got following snippiest. // lvalues: // int i = 42; i = 43; // ok, i is an lvalue int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo() is an lvalue // rvalues: // int foobar(); int j = 0; j = foobar(); // ok, foobar() is an rvalue int* p2 = &foobar(); // error, cannot ta

右值和左值之间的确切区别

当我阅读http://thbecker.net/articles/rvalue_references/section_01.html时,我得到了以下snippiest。 // lvalues: // int i = 42; i = 43; // ok, i is an lvalue int& foo(); foo() = 42; // ok, foo() is an lvalue int* p1 = &foo(); // ok, foo() is an lvalue // rvalues: // int foobar(); int j = 0; j = foobar(); // ok, foobar() is an rvalue int* p2 = &foobar(); // error, cannot take the addres

c++

I've been reading quite many on the Internet and it seems that many people mentioned the following rules (but i couldn't find it in the standard), The addition operator + (and all other binary operators) requires both operands to be rvalue, and the result is rvalue. And so on.. I checked the C++ standard, and it clearly states that (clause 3.10/2), Whenever a glvalue appears in a c

C ++

我在互联网上阅读了很多,似乎很多人提到了以下规则(但我无法在标准中找到它), 加法运算符+(以及所有其他二元运算符)要求两个操作数都是右值,结果是右值。 等等.. 我检查了C ++标准,并明确指出(第3.10 / 2条), 每当一个glvalue出现在预期prvalue的上下文中时,该glvalue将被转换为一个prvalue (第5/9条), 每当一个glvalue表达式作为一个操作符的操作数出现,该操作数需要该操作数的一个值时,左值到右值

Does C++ do value initialization of a POD typedef?

Does C++ do value initialization on simple POD typedefs? Assuming typedef T* Ptr; does Ptr() do value-initialization and guarantee to equal (T*)0 ? eg Ptr p = Ptr(); return Ptr(); It does. For a type T , T() value-initializes an "object" of type T and yields an rvalue expression. int a = int(); assert(a == 0); Same for pod-classes: struct A { int a; }; assert(A().a == 0);

C ++是否确定了POD typedef的初始化值?

C ++是否对简单的POD typedefs进行初始化? 假设 typedef T* Ptr; 不 Ptr() 做值初始化并保证等于(T*)0 ? 例如 Ptr p = Ptr(); return Ptr(); 它的确如此。 对于T类型, T()值 - 初始化T类型的“对象”并生成右值表达式。 int a = int(); assert(a == 0); 相同的Pod类: struct A { int a; }; assert(A().a == 0); 对于一些没有用户声明的构造函数的非POD类也是如此: struct A { ~A() { } int a; }; assert(A().a

class rvalues always have cv

§3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvaluen"; } void pass_int(const int&& i) { std::cout << "const rvaluen"; } int main() { pass_int(foo()); // prints "rvalue" pass_int(bar());

等级rvalues总是有cv

§3.10第9节规定“非等级rvalues总是具有cv不合格类型”。 这让我想知道... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvaluen"; } void pass_int(const int&& i) { std::cout << "const rvaluen"; } int main() { pass_int(foo()); // prints "rvalue" pass_int(bar()); // prints "const rvalue" } 根据标准,

Does my source code shows my own understanding of structures in c++?

I'd like to know if I am in fact in the right direction, I am currently learning the C++ language and reading this book called Jumping into C++ by Alex Allain and there's a practice problem at the end of the chapter regarding structures, to create a contact book program the user should be able to not just fill out a single structure, but should be able to add new entries, each with a sepa

我的源代码是否显示了我对c ++中结构的理解?

我想知道如果我事实上是朝着正确的方向发展的,我目前正在学习C ++语言,并阅读由Alex Allain撰写的名为“跳进C ++”的书,并且在本章末尾有一个关于结构的实践问题,一个联系簿程序用户应该不能只填写一个结构,但应该能够添加新的条目,每个条目都有一个单独的名称和电话号码。 让用户添加尽可能多的条目 - 这很容易吗? 添加显示全部或部分条目的功能,让用户浏览条目列表。 到目前为止,以下是我所做的,我想知道我的源代

Is the sizeof(some pointer) always equal to four?

For example: sizeof(char*) returns 4. As does int* , long long* , everything that I've tried. Are there any exceptions to this? The guarantee you get is that sizeof(char) == 1 . There are no other guarantees, including no guarantee that sizeof(int *) == sizeof(double *) . In practice, pointers will be size 2 on a 16-bit system (if you can find one), 4 on a 32-bit system, and 8 on a 64-b

sizeof(某个指针)总是等于四?

例如: sizeof(char*)返回4.像int* , long long* ,我试过的所有东西。 这有没有例外? 你得到的保证是sizeof(char) == 1 。 没有其他保证,包括不保证sizeof(int *) == sizeof(double *) 。 实际上,在16位系统上(如果可以找到一个),指针的大小为2,32位系统上为4,而64位系统上为8,但依靠给定的指针无法获得尺寸。 即使在普通的x86 32位平台上,您也可以获得各种指针大小,请尝试以下示例: struct A {}; struct

Why should copy constructors be sometimes declared explicitly non

I have trouble understanding the sentence with respect to inline and customers binary compatibility. Can someone please explain? C++ FAQ Cline, Lomow: When the compiler synthesizes the copy constructor, it makes them inline. If your classes are exposed to your customers ( for example, if your customers #include your header files rather than merely using an executable, built from your classe

为什么应该拷贝构造函数有时被声明为非显式的

我无法理解关于内联和客户二进制兼容性的句子。 有人可以解释吗? C ++常见问题解答Cline,Lomow: 当编译器合成复制构造函数时,它使它们内联。 如果你的类暴露给你的客户(例如,如果你的客户#包括你的头文件而不是仅仅使用从你的类构建的可执行文件),你的内联代码将被复制到你的客户可执行文件中。 如果您的客户希望在头文件发布之间保持二进制兼容性,则不得更改客户可见的内联函数。 因此,您需要一个显式的非内

Is the pImpl idiom really used in practice?

I am reading the book "Exceptional C++" by Herb Sutter, and in that book I have learned about the pImpl idiom. Basically, the idea is to create a structure for the private objects of a class and dynamically allocate them to decrease the compilation time (and also hide the private implementations in a better manner). For example: class X { private: C c; D d; } ; could be chang

pImpl习语在实践中是否真的被使用?

我正在阅读Herb Sutter的书“Exceptional C ++”,并且在那本书中我学习了pImpl习语。 基本上,这个想法是为一个class的private对象创建一个结构并动态地分配它们来减少编译时间 (并且以更好的方式隐藏私有实现)。 例如: class X { private: C c; D d; } ; 可以更改为: class X { private: struct XImpl; XImpl* pImpl; }; 并在CPP中定义: struct X::XImpl { C c; D d; }; 这看起来很有趣,但

Developing as a programmer

I have been learning C++ for three months now and in that time created a number of applications for my company. I consider myself fairly comfortable with C++ / MFC and STL, however I don't just want to be an OK programmer, I want to be a good programmer. I have a few books on best practices but I was wondering if anyone could suggest reading materials that helped them and any disciplines wh

作为程序员开发

我现在已经学习了三个月的C ++,并在那段时间为我的公司创建了一些应用程序。 我认为自己对C ++ / MFC和STL很满意,但我不只是想成为一名好的程序员,我想成为一名优秀的程序员。 我有几本关于最佳实践的书,但我想知道是否有人可以建议阅读帮助他们的材料以及应该鼓励的任何学科? 谢谢! 对于C ++来说,斯科特迈尔斯的书很好,可以帮助你进入下一个阶段。 如果你还没有Bjarne Stroustrup的第三版C ++ 我将从实用程序员