c++03

Replacement for ternary operator in template metaprogramming

自古美人都是妖i 提交于 2019-12-12 10:13:17
问题 I am implementing a binomial coefficient (n choose k) function in C++. Besides using a "normal" function (which is evaluated at runtime) this also can be accomplished using template metaprogramming (when the arguments are known at compile time): template <unsigned int n, unsigned int k> struct Binomialkoeffizient { static const unsigned int value = Binomialkoeffizient<n, k-1>::value * (n-k+1) / k; }; template <unsigned int n> struct Binomialkoeffizient<n, 0> { static const unsigned int value

How to specify “all parameterized types” or “all argument lists” for a dynamic_cast?

為{幸葍}努か 提交于 2019-12-12 04:21:38
问题 I'm trying to call a method on a derived class. The derived class has template parameters, but I don't know what they are. How do specify “all parameterized types” or “all argument lists” for a dynamic_cast ? Below is the MCVE, but here's the thrust of the problem: // Contrived, but close approximation DerivedOne<X> one; // Another one DerivedTwo<X,Y> two; // Maybe another one somewhere... DerivedTwo<X,Z> three; // Here's the problem Base& base = dynamic_cast<Base&>(two); if (base.HasOp()) {

c++ passing class method as argument to a class method with templates

无人久伴 提交于 2019-12-11 12:22:30
问题 I'm trying to pass a class method to another class method using template, and cannot find any answer on how to do (no C++11, boost ok): I simplified the core problem to : class Numerical_Integrator : public Generic Integrator{ template <class T> void integrate(void (T::*f)() ){ // f(); //already without calling f() i get error } } class Behavior{ void toto(){}; void evolution(){ Numerical_Integrator my_integrator; my_integrator->integrate(this->toto}; } I get as error: error: no matching

C++03 linker “already defined symbol” doesn't appear on intermediate file

你。 提交于 2019-12-11 12:19:28
问题 I am having a problem with a large project on visual studio 2005 on which I have run out of ideas. I can't even put a working code snippet because I don't know what's related, but I will try: I needed to make each .cpp file in my project have its own ID number, and create an instance of an object (which is globally accessible) that knows that ID. I followed the help on the accepted answer on this thread How to manage file unique IDs in c++ and made it work in a sandbox environment. Adding

Is it legal to have statically-allocated pure-virtual-parent-class references to statically-allocated child class objects?

旧巷老猫 提交于 2019-12-11 09:32:45
问题 UPDATE: I am not asking people to try this out & see if the code works for them. I am asking whether the code pattern is legal C++, regardless of whether it works for you. I am investigating what I believe is a bug in the IAR C++ compiler for Renesas RX CPUs. Examples of the following code pattern sometimes work on my small embedded system, other times it crashes during initialization of parentRefToChildInstance in a jump to address 0x00000000 (or nearby; I've seen a jump to 0x00000038 as

Find the Index of the Selected Object

我只是一个虾纸丫 提交于 2019-12-11 06:44:52
问题 Given an object Foo which has the method bool isChecked() const . Let's say that I have Foo foos[] . I am guaranteed that exactly one element of foos will return true on isChecked() , all others will return false . I'm looking for a clever C++03 way to find the index of the true element. I can do this but it's pretty ugly. Does anyone have something better? distance(foos, find_if(foos, foos + SIZE, mem_fun_ref(&Foo::isChecked))) #include <algorithm> #include <functional> #include <iostream>

Conditionally inheriting from either of two classes [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-11 05:06:34
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Generating Structures dynamically at compile time I am now facing a situation where I want a derived class to inherit from either Base1 or Base2 depending on a condition (in C++03). This means, I want to implement something like: // pseudo-C++ code class Derived : public if(condition) Base1 // inherit from Base1, if condition is true else Base2 // else inherit from Base2 { /* */ }; This is likely not a good

c++03 Initializing a array of objects with multiple parameters

匆匆过客 提交于 2019-12-11 00:19:33
问题 This might be a simple question but I am trying to initialize an array of objects using a parameterized constructor. For example: class A{ public: int b,c,d; A (int i, int j); }; void A::A(int i, int j){ d = rand() b = 2*i; c = 3*j; } void main(){ A a[50]; /*Initialize the 50 objects using the constructor*/ } I have already tried with vector initialization as mentioned in this link however, since there are 2 parameters, this does not work. Also, as mentioned in this link, it is not possible

Check for function signature also for inherited functions

橙三吉。 提交于 2019-12-10 18:12:33
问题 I need to check, if a containers erase function returns the iterator. I'd normally check for the function signature via e.g. boost. But in case of a boost class (e.g. flat_set) erase is inherited and thus not found by the check. But I really need it. SFINAE to check for inherited member functions only shows a C++11 solution which I can't use yet. I tried something like this: template <typename T> class has_member_func_erase_it_constit { typedef typename T::iterator iterator; typedef typename

Sign of C++ Enum Type Incorrect After Converting to Integral Type

心已入冬 提交于 2019-12-10 17:43:23
问题 My understanding is that C++ enumerations are converted to integral types according to Integral Promotion. And during Integral Promotion, we should try converting a value to int first and if the value cannot be represented by an int , unsigned int should be used: C++03 conv.prom: 2) .... An rvalue of an enumeration type (7.2) can be converted to an rvalue of the first of the following types that can represent all the values of the enumeration (i.e. the values in the range bmin to bmax as