c++-faq

Restrict variadic template arguments

北战南征 提交于 2019-11-26 15:23:56
问题 Can we restrict variadic template arguments to a certain type? I.e., achieve something like this (not real C++ of course): struct X {}; auto foo(X... args) Here my intention is to have a function which accepts a variable number of X parameters. The closest we have is this: template <class... Args> auto foo(Args... args) but this accepts any type of parameter. 回答1: Yes it is possible. First of all you need to decide if you want to accept only the type, or if you want to accept a implicitly

Different floating point result with optimization enabled - compiler bug?

﹥>﹥吖頭↗ 提交于 2019-11-26 15:12:18
The below code works on Visual Studio 2008 with and without optimization. But it only works on g++ without optimization (O0). #include <cstdlib> #include <iostream> #include <cmath> double round(double v, double digit) { double pow = std::pow(10.0, digit); double t = v * pow; //std::cout << "t:" << t << std::endl; double r = std::floor(t + 0.5); //std::cout << "r:" << r << std::endl; return r / pow; } int main(int argc, char *argv[]) { std::cout << round(4.45, 1) << std::endl; std::cout << round(4.55, 1) << std::endl; } The output should be: 4.5 4.6 But g++ with optimization ( O1 - O3 ) will

Why would one replace default new and delete operators?

…衆ロ難τιáo~ 提交于 2019-11-26 14:56:35
Why should would one replace the default operator new and delete with a custom new and delete operators? This is in continuation of Overloading new and delete in the immensely illuminating C++ FAQ: Operator overloading. An followup entry to this FAQ is: How should I write ISO C++ standard conformant custom new and delete operators? Note: The answer is based on lessons from Scott Meyers' More Effective C++. (Note: This is meant to be an entry to Stack Overflow's C++ FAQ . If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be

Why is `std::move` named `std::move`?

[亡魂溺海] 提交于 2019-11-26 14:02:39
The C++11 std::move(x) function doesn't really move anything at all. It is just a cast to r-value. Why was this done? Isn't this misleading? It is correct that std::move(x) is just a cast to rvalue - more specifically to an xvalue , as opposed to a prvalue . And it is also true that having a cast named move sometimes confuses people. However the intent of this naming is not to confuse, but rather to make your code more readable. The history of move dates back to the original move proposal in 2002 . This paper first introduces the rvalue reference, and then shows how to write a more efficient

Semantics of flags on basic_ios

主宰稳场 提交于 2019-11-26 13:05:35
I find myself repeatedly baffled by the rdstate() flags - good() , bad() , eof() , fail() - and how they are expressed in basic_ios::operator! , operator bool and operator void* . Could somebody put me out of my misery and explain this so I never have to think twice again? James McNellis There are three flags that indicate error state: badbit means something has gone very wrong with the stream. It might be a buffer error or an error in whatever is feeding data to the stream. If this flag is set, it's likely that you aren't going to be using the stream anymore. failbit means that an extraction

Why doesn&#39;t a derived template class have access to a base template class&#39; identifiers?

久未见 提交于 2019-11-26 12:53:31
Consider: template <typename T> class Base { public: static const bool ZEROFILL = true; static const bool NO_ZEROFILL = false; } template <typename T> class Derived : public Base<T> { public: Derived( bool initZero = NO_ZEROFILL ); // NO_ZEROFILL is not visible ~Derived(); } I am not able compile this with GCC g++ 3.4.4 (cygwin). Prior to converting these to class templates, they were non-generic and the derived class was able to see the base class's static members. Is this loss of visibility in a requirement of the C++ spec or is there a syntax change that I need to employ? I understand that

How to get IOStream to perform better?

我怕爱的太早我们不能终老 提交于 2019-11-26 12:41:35
Most C++ users that learned C prefer to use the printf / scanf family of functions even when they're coding in C++. Although I admit that I find the interface way better (especially POSIX-like format and localization), it seems that an overwhelming concern is performance. Taking at look at this question: How can I speed up line by line reading of a file It seems that the best answer is to use fscanf and that the C++ ifstream is consistently 2-3 times slower. I thought it would be great if we could compile a repository of "tips" to improve IOStreams performance, what works, what does not.

Convert string to int with bool/fail in C++

瘦欲@ 提交于 2019-11-26 12:24:26
I have a std::string which could be a string or could be a value (such as 0 ). What is the best or easiest way to convert the std::string to int with the ability to fail? I want a C++ version of C#'s Int32.TryParse . Use boost::lexical_cast . If the cast cannot be done, it will throw an exception . #include <boost/lexical_cast.hpp> #include <iostream> #include <string> int main(void) { std::string s; std::cin >> s; try { int i = boost::lexical_cast<int>(s); /* ... */ } catch(...) { /* ... */ } } Without boost: #include <iostream> #include <sstream> #include <string> int main(void) { std:

Why do we actually need Private or Protected inheritance in C++?

被刻印的时光 ゝ 提交于 2019-11-26 12:18:06
问题 In C++, I can\'t think of a case in which I would like to inherit private/protected from a base class: class Base; class Derived1 : private Base; class Derived2 : protected Base; Is it really useful? 回答1: It is useful when you want to have access to some members of the base class, but without exposing them in your class interface. Private inheritance can also be seen as some kind of composition: the C++ faq-lite gives the following example to illustrate this statement class Engine { public:

Why is &#39;this&#39; a pointer and not a reference?

人盡茶涼 提交于 2019-11-26 12:05:02
I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments. programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type std::string but evaluates to a char const* (pointer) (after array to pointer conversion) – Johannes Schaub - litb Dec 22 '08 at 1:56 That only shows that it doesn't use the same conventions as other (later) languages. – le dorfier Dec 22 '08 at 3:35 I'd call the "this" thing a pretty trivial issue though. And oops, thanks for catching a few errors in my