-2

I was wondering how could I do something when some boolean expression becomes true. I can't use a simple if, because I want that it checks when the bools expressions changes in any part of code and do it if it is true.

It would be something like this:

when(int x != 0)
{
  cout << "x";
}

It could use Assembly to, the better the performance the best.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Rafael Marinho
  • 100
  • 1
  • 10

4 Answers4

2

I was wondering how could I do something when some boolean expression is true

That's exactly what if does.

I want that it checks when the bools expressions changes in any part of code

Simply use if every time you assign the variable.

Of course, when you always do the same check after assigning the variable and do the same thing if the check is true, then you clearly have repetition. When you want to repeatedly do the same thing multiple times, you can use a function to avoid repeating the code. Just don't accidentally modify the variable directly instead of calling that function.

Here's an example of a function that assigns a variable in memory and does a branch based on a boolean expression. If true, it prints the variable:

                    .cfi_startproc
                    @ args = 0, pretend = 0, frame = 8
                    @ frame_needed = 1, uses_anonymous_args = 0
0000 00482DE9       stmfd   sp!, {fp, lr}
                    .save {fp, lr}

                .LCFI0:

                    .cfi_def_cfa_offset 8
                    .cfi_offset 11, -8
                    .cfi_offset 14, -4
                    .setfp fp, sp, #4
0004 04B08DE2       add fp, sp, #4

                .LCFI1:

                    .cfi_def_cfa 11, 4
                    .pad #8
0008 08D04DE2       sub sp, sp, #8
000c 08000BE5       str r0, [fp, #-8]

0010 30309FE5       ldr r3, .L3
0014 08201BE5       ldr r2, [fp, #-8]
0018 002083E5       str r2, [r3, #0]

001c 24309FE5       ldr r3, .L3
0020 003093E5       ldr r3, [r3, #0]
0024 000053E3       cmp r3, #0
0028 0400001A       bne .L1           ; <-- the branch is here

002c 14309FE5       ldr r3, .L3
0030 003093E5       ldr r3, [r3, #0]
0034 10009FE5       ldr r0, .L3+4
0038 0310A0E1       mov r1, r3
003c FEFFFFEB       bl  printf

                .L1:

0040 04D04BE2       sub sp, fp, #4
0044 0088BDE8       ldmfd   sp!, {fp, pc}

                .L4:

                    .align  2

                .L3:

0048 00000000       .word   global
004c 00000000       .word   .LC0
                    .cfi_endproc
eerorika
  • 232,697
  • 12
  • 197
  • 326
2

If this is for debugging, set a data-breakpoint, or watch-point, on x in your debugger.

If not, then you're using C++, so you can wrap x in a class that overloads the assignment operator to prints changes. Overload operator const &int() to allow stuff like foo = x + 4, without needing foo = x.get_value() + 4.

#include <iostream>

template <typename T>
class value_changelogger
{
  private:
    T m_val;
  public:
    T& operator=(T newval) {
      if (newval != m_val)
        std::cout << newval << std::endl;
      return m_val = newval;
    }
    operator const T&() { return m_val; }  // only allow const-references to escape our watchful eye
};

value_changelogger<int> x;

int main(int, char**)
{
    x = 4;
    int y = x + 4;
    x = y;
    // x++;  doesn't compile: no operator++ overload, and non-const operator T&() isn't overloaded.
}

(wandbox link)

C++11 explicit operator T& doesn't appear to help for change-detection without actually having code for all the modify operators.

So I think you'd have to individually overload all the operators that can modify the value, to have this fully transparent.

Note that this is just syntactic sugar to add in an if () check everywhere x is assigned. It doesn't make the machine code any more efficient than if you'd gone and manually done that, like you'd have to in C.

Community
  • 1
  • 1
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    I'm sure someone's written a complete wrapper for integer types somewhere... altho can't seem to find it by googling – M.M Dec 04 '15 at 23:14
1

This is not directly possible with C++, because all statements are executed sequentially. There is no "C++ data breakpoint", like debuggers offer it.

Assuming you don't want to change all the places where the variable is changed, the closest you can get is to create a second thread that repeatedly checks the boolean condition, and executes the action when the condition is met. But the action will never be executed immediately after the condition has changed, there will always be a short delay.

alain
  • 11,939
  • 2
  • 31
  • 51
0

You can use:

int _x;
inline int get_X(){return _x;}
inline int set_X(int value){
    _x = value;
    //dosomething
    return _x;
}

and then instead of writing x = bla in your code, write set_X(bla).

M.M
  • 138,810
  • 21
  • 208
  • 365
AntiHeadshot
  • 1,130
  • 9
  • 24