How to get compile time error checking with real valued function arguments?

fuse117

Is it possible use static_assert (or something similar) with function arguments that are real valued? What I am trying to do is the following. The arguments min and max will always be constant in my application. Ideally, I would like to use them as template parameters, but doing so isn't possible because they are real valued. The motivation for using static_assert is that I would like to get compile time error checking.

template <typename counts_t, typename real_t>
real_t counts2real(counts_t c, real_t min, real_t max)
{
  constexpr real_t cmin = std::numeric_limits<counts_t>::min();
  constexpr real_t cmax = std::numeric_limits<counts_t>::max();
  constexpr real_t cdelta = (cmax - cmin);

  // ERROR: non-constant condition for static assertion.
  static_assert(max > min, "max > min");

  real_t delta = (max - min);
  real_t p = (c - cmin) / cdelta;

  return (p * delta + min);
}

int16_t x = 0;
const float min = 10.0;
const float max = 5.0;

float xf = counts2real<int16_t,float>(x, min, max);
Justin

While floats cannot be used as template parameters, float const&s can. Thus, you can pass min and max as template parameters:

template <typename real_t, real_t const& min, real_t const& max, typename counts_t>
real_t counts2real(counts_t c)
{
  constexpr real_t cmin = std::numeric_limits<counts_t>::min();
  constexpr real_t cmax = std::numeric_limits<counts_t>::max();
  constexpr real_t cdelta = (cmax - cmin);

  static_assert(max > min, "max > min");

  real_t delta = (max - min);
  real_t p = (c - cmin) / cdelta;

  return (p * delta + min);
}

Usage:

constexpr float min = 10.0;
constexpr float max = 50.0;

float foo(int16_t x) {
    return counts2real<float, min, max>(x);
}

Changing max to 5.0 results in a diagnostic as desired:

<source>:13:21: error: static assertion failed: max > min
   static_assert(max > min, "max > min");

Demo


In C++17, you can avoid having to specify the type of min and max:

template <auto const& min, auto const& max, typename counts_t>
constexpr auto counts2real(counts_t c)
{
  ...
}

// ...
float foo(int16_t x) {
    return counts2real<min, max>(x);
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I get real time script error notification when using a google sheets onOpen() function that errors out?

分類Dev

ASIO handler arguments and boost::bind, compile time error

分類Dev

How to get the arguments to a php function via grep?

分類Dev

Differentiating alias and real-types at compile time?

分類Dev

How get in real time POST update with django and instagram api?

分類Dev

c++ compile time error

分類Dev

Compile time error in C#

分類Dev

How to compile program with _mm_clflushopt function? error: inlining failed

分類Dev

How to get the name of decorator for function at run time?

分類Dev

How to define a matrix valued function in Matlab/Octave -- return value syntax

分類Dev

Go compile error "undefined function"

分類Dev

How to calculate a One-Hot Encoding value into a real-valued vector?

分類Dev

Why do I get 'unknown type name DetailViewController' error, only some of the time when I compile?

分類Dev

How is a vptr initialized at compile time?

分類Dev

Python decorator function called at compile time

分類Dev

C# UWP how to type a number string and get commas real time in order?

分類Dev

How to create real-time changes in JavaScript

分類Dev

How to add real time search with VueJS?

分類Dev

How to parse arguments to a function in Python?

分類Dev

How to evaluate function arguments with for loop

分類Dev

"This function takes no arguments" error when calling UDF

分類Dev

Awkward PHP type error in function arguments

分類Dev

How to fix Fatal error: Uncaught ArgumentCountError: Too few arguments to function Login::login(), 0 passed?

分類Dev

How to enforce that a type implements a trait at compile time?

分類Dev

How to order types at compile-time?

分類Dev

how to check the size of an array during compile time

分類Dev

How to tell if expression is evaluated at compile time or runtime?

分類Dev

How to check at compile time if type is polymorhic

分類Dev

How to run bash command with arguments at scheduled time

Related 関連記事

  1. 1

    How can I get real time script error notification when using a google sheets onOpen() function that errors out?

  2. 2

    ASIO handler arguments and boost::bind, compile time error

  3. 3

    How to get the arguments to a php function via grep?

  4. 4

    Differentiating alias and real-types at compile time?

  5. 5

    How get in real time POST update with django and instagram api?

  6. 6

    c++ compile time error

  7. 7

    Compile time error in C#

  8. 8

    How to compile program with _mm_clflushopt function? error: inlining failed

  9. 9

    How to get the name of decorator for function at run time?

  10. 10

    How to define a matrix valued function in Matlab/Octave -- return value syntax

  11. 11

    Go compile error "undefined function"

  12. 12

    How to calculate a One-Hot Encoding value into a real-valued vector?

  13. 13

    Why do I get 'unknown type name DetailViewController' error, only some of the time when I compile?

  14. 14

    How is a vptr initialized at compile time?

  15. 15

    Python decorator function called at compile time

  16. 16

    C# UWP how to type a number string and get commas real time in order?

  17. 17

    How to create real-time changes in JavaScript

  18. 18

    How to add real time search with VueJS?

  19. 19

    How to parse arguments to a function in Python?

  20. 20

    How to evaluate function arguments with for loop

  21. 21

    "This function takes no arguments" error when calling UDF

  22. 22

    Awkward PHP type error in function arguments

  23. 23

    How to fix Fatal error: Uncaught ArgumentCountError: Too few arguments to function Login::login(), 0 passed?

  24. 24

    How to enforce that a type implements a trait at compile time?

  25. 25

    How to order types at compile-time?

  26. 26

    how to check the size of an array during compile time

  27. 27

    How to tell if expression is evaluated at compile time or runtime?

  28. 28

    How to check at compile time if type is polymorhic

  29. 29

    How to run bash command with arguments at scheduled time

ホットタグ

アーカイブ