c++-faq

Template specialization of particular members?

帅比萌擦擦* 提交于 2019-11-26 05:27:21
问题 Is it possible to specialize particular members of a template class? Something like: template <typename T,bool B> struct X { void Specialized(); }; template <typename T> void X<T,true>::Specialized() { ... } template <typename T> void X<T,false>::Specialized() { ... } Ofcourse, this code isn\'t valid. 回答1: You can only specialize it explicitly by providing all template arguments. No partial specialization for member functions of class templates is allowed. template <typename T,bool B> struct

Does std::list::remove method call destructor of each removed element?

人走茶凉 提交于 2019-11-26 05:27:15
问题 I have the code: std::list<Node *> lst; //.... Node * node = /* get from somewhere pointer on my node */; lst.remove(node); Does the std::list::remove method call the destructor (and free memory) of each removed element? If so, how I can avoid it? 回答1: Yes, removing a Foo* from a container destroys the Foo* , but it will not release the Foo . Destroying a raw pointer is always a no-op. It cannot be any other way! Let me give you several reasons why. Storage class Deleting a pointer only makes

Why are my struct&#39;s members not properly initialised using `{}`? [duplicate]

天大地大妈咪最大 提交于 2019-11-26 04:26:15
问题 This question already has an answer here: C and C++ : Partial initialization of automatic structure 5 answers I had the following code: #include <iostream> struct T { int a, b, c; }; int main() { T t = {0}; std::cout << t.a << \',\' << t.b << \',\' << t.c << \'\\n\'; } Output: 0,0,0 After many years of this code running happily in a critical production environment, serving a vital function, the requirements of the project changed and I needed the output to be 1,1,1 . So, I changed {0} to {1}

Different floating point result with optimization enabled - compiler bug?

人走茶凉 提交于 2019-11-26 03:53:51
问题 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 <

Semantics of flags on basic_ios

≡放荡痞女 提交于 2019-11-26 03:38:38
问题 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? 回答1: 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

How can I efficiently select a Standard Library container in C++11?

删除回忆录丶 提交于 2019-11-26 03:24:43
问题 There\'s a well known image (cheat sheet) called \"C++ Container choice\". It\'s a flow chart to choose the best container for the wanted usage. Does anybody know if there\'s already a C++11 version of it? This is the previous one: 回答1: Not that I know of, however it can be done textually I guess. Also, the chart is slightly off, because list is not such a good container in general, and neither is forward_list . Both lists are very specialized containers for niche applications. To build such

What C++ Smart Pointer Implementations are available?

戏子无情 提交于 2019-11-26 03:23:20
问题 Comparisons, Pros, Cons, and When to Use? This is a spin-off from a garbage collection thread where what I thought was a simple answer generated a lot of comments about some specific smart pointer implementations so it seemed worth starting a new post. Ultimately the question is what are the various implementations of smart pointers in C++ out there and how do they compare? Just simple pros and cons or exceptions and gotchas to something you might otherwise think should work. I\'ve posted

What is a “span” and when should I use one?

喜夏-厌秋 提交于 2019-11-26 03:15:36
问题 Recently I\'ve gotten suggestions to use span<T> \'s in my code, or have seen some answers here on the site which use span \'s - supposedly some kind of container. But - I can\'t find anything like that in the C++ standard library. So what is this mysterious span<T> , and why (or when) is it a good idea to use it if it\'s non-standard? 回答1: What is it? A span<T> is: A very lightweight abstraction of a contiguous sequence of values of type T somewhere in memory. Basically a struct { T * ptr;

What are the rules about using an underscore in a C++ identifier?

自闭症网瘾萝莉.ら 提交于 2019-11-26 03:12:35
问题 It\'s common in C++ to name member variables with some kind of prefix to denote the fact that they\'re member variables, rather than local variables or parameters. If you\'ve come from an MFC background, you\'ll probably use m_foo . I\'ve also seen myFoo occasionally. C# (or possibly just .NET) seems to recommend using just an underscore, as in _foo . Is this allowed by the C++ standard? 回答1: The rules (which did not change in C++11): Reserved in any scope, including for use as implementation

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

被刻印的时光 ゝ 提交于 2019-11-26 03:07:46
问题 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