c++03

Forward-declare a typedef

荒凉一梦 提交于 2019-12-10 17:23:49
问题 I have got a large header file (~10000 lines) which is auto-generated by a script/program out of my control. In order to avoid to include this file in the declaration of my class, I forward declare the few types I need: --myclass.h namespace bl { class TypeA; class TypeB; } // Other stuff and myclass definition... Now it turns out that TypeA and TypeB are not class-names, but are instead defined inside the auto-generated file as: typedef SomeUnspecifiedClassName TypeA; typedef

Initializing constant array of fixed size inside class

戏子无情 提交于 2019-12-10 17:14:49
问题 Consider the following class: class A { const int arr[2]; public: A() { } }; Is it possible to initialize arr from the constructor initializer list or in any other way than on the line where it is declared (i.e. const int arr[2] = {1,2}; )? Note that I'm interested in methods that work with C++98! 回答1: By wrapping them in a struct , e.g.: class A { struct Data { int arr[2]; }; Data const arr; public: A() : arr( someOtherStruct ) {} }; This does mean that to access the data, you'd have to

Do constant and reinterpret cast happen at compile time?

て烟熏妆下的殇ゞ 提交于 2019-12-10 15:52:57
问题 I have read that static_cast happens at compile time and dynamic_cast happens at run time thus are slower than static_cast . A dynamic_cast can either return a null ptr (when using with a pointer) or otherwise throw a bad cast exception. My question is what about reinterpret_cast and const_cast do they happen at compile time or run-time ? I would think that interpret cast happens at run-time since it behaves like dynamic_cast indicating if the cast was successful. Am i correct ? What about

Rvalues in C++03

爷,独闯天下 提交于 2019-12-10 15:35:21
问题 How can you tell whether or not a given parameter is an rvalue in C++03? I'm writing some very generic code and am in need of taking a reference if possible, or constructing a new object otherwise. Can I overload to take by-value as well as by-reference and have the rvalue returns call the by-value function? Or do I have a very sickening feeling that this is why rvalue references are in C++0x? Edit: is_rvalue = !(is_reference || is_pointer) ? 回答1: There apparently is a way to determine

C++ preprocessor conditional parameter

[亡魂溺海] 提交于 2019-12-10 14:45:53
问题 Please note C++03! any C++11 solutions are not good for me, but do post them just for knowledge sake. I know the preprocessor can do things like: #define FOO 4 #if FOO == 4 cout<<"hi"<<endl; #endif What I need is: #define BAR(X)\ #if X == 4\ cout<<"hi"<<endl;\ #endif main.cpp BAR(4) I don't see why all the needed information wouldn't be available in preprocessor time. So, Please tell me how to achieve this kind of behavior. edit 1: A normal if condition won't work for my case, because I also

How to “dereference a type” in C++03?

六眼飞鱼酱① 提交于 2019-12-10 14:34:52
问题 How do I get the "dereferenced type" of another type in C++03? Note that it can be other dereferenceable type like std::vector<int>::iterator . e.g. if I have template<typename T> struct MyPointer { T p; ??? operator *() { return *p; } }; How can I figure out what to replace the ??? with? ( No Boost ! I want to know how to figure it out myself.) 回答1: In the general case, you can't. For raw pointers, you can partially specialize as shown in other answers- custom smart pointers may have a

Retrieving size of datatype from std::type_info

删除回忆录丶 提交于 2019-12-10 12:59:07
问题 In C++03, when you use the operator typeid, a type_info object is returned. Is it possible to retrieve the size of the given type based only on this result, such as returned by the sizeof operator? For example: std::type_info info = typeid(int); int intSize = sizeof(int); int intSize2 = info.getSize(); // doesn't exist! The issue is that we use a third-party multi array class that gives back a type_info, but not the size of the type. 回答1: The best way I can see (I would like to be proven

Initialize union using largest member under MSVC compiler

流过昼夜 提交于 2019-12-10 10:59:00
问题 I'm trying to initialize a LARGE_INTEGER to 0 in a C++ library (C++03 to be exact). Previously, the initialization was: static LARGE_INTEGER freq = { 0 }; Under MinGW it produced a warning: missing initializer for member '_LARGE_INTEGER::::HighPart' So I changed the initialization to the following in accordance with Can a union be initialized in the declaration?: static LARGE_INTEGER freq = { .QuadPart = 0 }; I'm now testing under Visual Studio 2015, and its producing an error: 81 static

Using boost to create a lambda function which always returns true

最后都变了- 提交于 2019-12-10 06:01:28
问题 Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)> predicate ); If I want to call it with a predicate that always returns true, I can define a helper function: bool AlwaysTrue( int, int, int ) { return true; } ... Foo( boost::bind( AlwaysTrue ) ); But is there anyway to call this function (possibly using boost::lambda) without having to define a separate function? [Edit: forgot to say: I CAN'T use C++0x] 回答1: UncleBens commented on this

C++ “Floating Point Enum”

醉酒当歌 提交于 2019-12-09 13:05:14
问题 I am looking for a solution using the C++03 standard (I am constrained to using this version of the standard for several years yet). Solutions for C++11 are also welcome, but will not be "accepted" as the answer to this question. What is a simple, concise way that I can represent a set of related constant floating point values as a single type (similar to an enum) to ensure type-safety without incurring significant overhead and still allow me to operate on the values as floats directly? The