c++98

implement factory pattern for products with conditional compiling

本秂侑毒 提交于 2019-12-10 15:26:31
问题 I'd like to implement factory (or some other pattern) in a way that will allow me to compile the code without introducing type dependency. enum CarType { BMW, PORSCHE, MERC }; class CarFactory { public: static Car* create(CarType type) { switch(type) { case BMW : return new BMWCar(); case PORSCHE : return new PorscheCar(); default : return new MercCar(); } } }; When I compile CarFactory, I need to include BMWCar, PorscheCar and MercCar as a part of my compilation/linking unit. The way my

Enforcing the C++98 standard in gcc

好久不见. 提交于 2019-12-10 14:44:28
问题 I have a school assignment that should be written in C++98-compliant code. How can I force g++ to accept only code that follows this standard? Will -std=c++98 do the trick or do I need to add additional flags? 回答1: Per GCC's documentation on standards: The original ISO C++ standard was published as the ISO standard (ISO/IEC 14882:1998) and amended by a Technical Corrigenda published in 2003 (ISO/IEC 14882:2003). These standards are referred to as C++98 and C++03, respectively. GCC implements

template magic for wrapping C callbacks that take void* parameters?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 02:13:44
问题 Say I'm using a C API that lets you register callbacks that take a void* closure: void register_callback(void (*func)(void*), void *closure); In C++ it's nice to have stronger types than void* so I want to create a wrapper that lets me register strongly-typed C++ callbacks instead: template <typename T, void F(T*)> void CallbackWrapper(void *p) { return F(static_cast<T*>(p)); } void MyCallback(int* param) {} void f(void *closure) { register_callback(CallbackWrapper<int, MyCallback>, closure);

int a=int(); what happens in C++98?

微笑、不失礼 提交于 2019-12-09 16:30:36
问题 Please read the question entirely before you think to mark it as duplicate. The statement like int i=int(); most programmers will say that there is value initialization here & i will be value initialized. (0 as output). But it also prints 0 as output on C++98 compiler. Following program that I tested on C++98 implementation and gives me 0 as output. #include <iostream> int main() { int i=int(); std::cout<<i; } Don't say that i is value initialized in above C++98 program ,because value

Behavior of self-assignment with const ref parameter

怎甘沉沦 提交于 2019-12-09 03:06:32
问题 I stumbled upon some very old code which has a class with a defined copy assignment operator which takes its parameter as a const reference, but also does not check for self-assignment, so essentially: struct A { int q; A(): q(3) {} A& operator=(const A& a) { q = a.q; return *this; } }; What is the behavior of this assignment operator when an instance of A is assigned to itself? I would assume this causes problems as it "breaks" the constness of the paramater, any compiler could assume that

How to check if a template type is one of the types of a variant type?

本秂侑毒 提交于 2019-12-08 19:31:50
问题 Considering a variant type and a template function, how can I check the template type is one of the types of the variant ? Is there a more elegant way than the following ? typedef boost::variant<Foo,Bar> Var; template <typename T> void f(const T& x) { BOOST_STATIC_ASSERT( boost::is_same<T,Foo>::value || boost::is_same<T,Bar>::value ); } Note : I use Boost 1.57 and gcc 4.8.3. I don't use C++11 for compatibility with old gcc versions. 回答1: Use MPL: #include <boost/variant/variant.hpp> #include

Is value initialization part of the C++98 standard? If not, why was it added in the C++03 standard?

断了今生、忘了曾经 提交于 2019-12-08 15:15:14
问题 Cheers and hth. - Alf made a comment in this answer that value initialization is arguably a new feature of C++03 compared to C++98. I wonder what he meant. Is value initialization part of C++98? Is it present in concept but not in name? Why was it added to the C++03 standard? I have a copy of the '03 standard but not the '98 standard. Here's the definition of default initialization and value initialization. To default-initialize an object of type T means: — if T is a non-POD class type

Passing a map between classes

烂漫一生 提交于 2019-12-08 05:06:17
问题 This is in relation to an earlier question I had. I haven't managed to solve the problem there but for now I'm just trying to get better acquainted with the code to figure out how to deal with that problem. Towards that goal, I've got around to trying out the suggestions given in that question and I'm a little stumped as to why the following isn't working. in the header I have class A { public: typedef std::multimap<int, double> intdoublemap_t; const intdoublemap_t& getMap() const; void

std::size_t or std::vector<Foo>::size_type?

て烟熏妆下的殇ゞ 提交于 2019-12-08 03:27:55
问题 When I loop on a std::vector<Foo> (or every container having random access iterator) I use an unsigned integer variable i . If I want to respect the norm, should I use std::size_t or the type given by the container itself : std::vector<Foo>::size_type ? If I chose std::size_t (for readability reasons), can I be sure that every implementation of every container in std namespace uses std::size_t as size_type ? Note : I use C++98 only (for compatibility reasons). 回答1: It is not necessarily true

In GCC, Clang and MSVC is there any way to conform against C++98 and not C++03?

大兔子大兔子 提交于 2019-12-07 02:17:10
问题 The meta question proposes that the c++98 and c++03 tags should be made synonyms. The question asker followed it up with Is value initialization part of the C++98 standard? If not, why was it added in the C++03 standard?, an excellent question which sheds light on the addition of value initialization to C++03. Consider this question to be a follow-up to the latter. The OP asserts that modern compilers do not bother distinguishing between C++98 and C++03. This was surprising to me, as it turns