How to eliminate "divide by 0" error in template code

Mark Ransom

I'm using a pair of integer template parameters to specify a ratio, since I can't use a double as a template parameter. The conversion into a double is protected against divide-by-zero with a ternary. This worked in an earlier version of the compiler, but Visual Studio 2013 gives an error:

error C2124: divide or mod by zero

Here's a simplified version of the code:

template<int B1, int B2>
class MyClass
{
    const double B = (B2 == 0) ? 0.0 : (double) B1 / (double) B2;
    // ...
};

MyClass<0, 0> myobj;

I really want B to be optimized out of expressions that use it when it's zero, so I need the single-line definition. I know I can just use template parameters <0, 1> to get around it, but I wonder if there's a way to just convince the compiler that my expression is safe?

Tony Delroy

What I'm told worked:

 const double B = (B2 == 0 ? 0.0 : (double) B1) /
                  (B2 == 0 ? 1.0 : (double) B2);

This avoids a reliance on short circuit evaluation preventing the divide by 0; having the conditional selections happen before the division.


Original idea / Perhaps something like this...? (I think B should be static const or constexpr, but I'm sure you can sort that...)

template<int B1, int B2>
struct MyClass
{
    const double B = (double) B1 / (double) B2;
};

template <int B1>
struct MyClass<B1, 0>
{
    const double B = 0.0;
};

If there's lots of other stuff you want in MyClass and don't want to duplicate or put in a base etc., you could move the B calculation into a supporting template using the specialisation approach above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to not let the user divide by 0?

From Dev

How to eliminate RTTI in sample code

From Dev

how to eliminate duplicate code in initmethod

From Dev

How to eliminate this loop from my Matlab code?

From Dev

How to eliminate repeat code in a for-loop?

From Dev

How to eliminate the duplicate values generated by this code

From Dev

Repeated code in two js functions, how to eliminate and make code elegant

From Dev

How to eliminate code repetition between different View Model modules?

From Dev

How to eliminate division inside code like "a/b>c/d"?

From Dev

How can i eliminate white spaces in my regex code?

From Dev

How can i eliminate white spaces in my regex code?

From Dev

How to do refactoring to eliminate type-code if it is used in validation rules?

From Dev

How to eliminate division inside code like "a/b>c/d"?

From Dev

How to eliminate if-then-else in CLIPS to better optimise code

From Dev

How do I eliminate an ACK slot error in CAN?

From Dev

How to eliminate error underlining in IntelliJ 14.0.3 for Play 2.3.7 application?

From Dev

How to eliminate and understand this error warning from vuetify data table slot?

From Dev

How to eliminate Free Pascal 'not recognized' compile error on TFileStream?

From Dev

Eliminate RootResourceURI root error

From Dev

Eliminate 404 error

From Dev

Trying to get a random number between two points and getting a divide by 0 error

From Dev

grails template Method code is too large error?

From Dev

grails template Method code is too large error?

From Dev

Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template resource

From Dev

Code To eliminate need to enter password

From Dev

Eliminate duplicated code in different classes

From Dev

Eliminate repeated code Swift 3

From Dev

How to eliminate this loop in R

From Dev

how to eliminate unneeded record

Related Related

  1. 1

    How to not let the user divide by 0?

  2. 2

    How to eliminate RTTI in sample code

  3. 3

    how to eliminate duplicate code in initmethod

  4. 4

    How to eliminate this loop from my Matlab code?

  5. 5

    How to eliminate repeat code in a for-loop?

  6. 6

    How to eliminate the duplicate values generated by this code

  7. 7

    Repeated code in two js functions, how to eliminate and make code elegant

  8. 8

    How to eliminate code repetition between different View Model modules?

  9. 9

    How to eliminate division inside code like "a/b>c/d"?

  10. 10

    How can i eliminate white spaces in my regex code?

  11. 11

    How can i eliminate white spaces in my regex code?

  12. 12

    How to do refactoring to eliminate type-code if it is used in validation rules?

  13. 13

    How to eliminate division inside code like "a/b>c/d"?

  14. 14

    How to eliminate if-then-else in CLIPS to better optimise code

  15. 15

    How do I eliminate an ACK slot error in CAN?

  16. 16

    How to eliminate error underlining in IntelliJ 14.0.3 for Play 2.3.7 application?

  17. 17

    How to eliminate and understand this error warning from vuetify data table slot?

  18. 18

    How to eliminate Free Pascal 'not recognized' compile error on TFileStream?

  19. 19

    Eliminate RootResourceURI root error

  20. 20

    Eliminate 404 error

  21. 21

    Trying to get a random number between two points and getting a divide by 0 error

  22. 22

    grails template Method code is too large error?

  23. 23

    grails template Method code is too large error?

  24. 24

    Error: Code=InvalidTemplate; Message=Deployment template validation failed: 'The template resource

  25. 25

    Code To eliminate need to enter password

  26. 26

    Eliminate duplicated code in different classes

  27. 27

    Eliminate repeated code Swift 3

  28. 28

    How to eliminate this loop in R

  29. 29

    how to eliminate unneeded record

HotTag

Archive