0

Is there any technique or compiler extension keyword to declare class member variables inside class member functions? Something like

struct test_t{
    void operator ()(){
        instance_local int i = 0;
    }
};

The best that came in my mind was using thread_local and then executing the member function inside another thread, but this would be too ugly to be useful.


EDIT: example

Well I'm really sorry for the following probably confusing example (it is related to my question yesterday Is there any problem in jumping into if(false) block?). I really tried to make a less confusing up...

#include <iostream>

#define instance_local thread_local

struct A{
    A(int i) :
        i(i)
    {

    }
    void dosomethinguseful(){
        std::cout << i << std::endl;
    }
    int i;
};
struct task1{
    int part;
    task1() : part(0){}
    void operator ()(){
        int result_of_calculation;
        switch (part) {
        case 0:{
            //DO SOME CALCULATION
            result_of_calculation = 5;
            instance_local A a(result_of_calculation);
            if(false) 
              case 1:{ a.dosomethinguseful();}
            part++;
        }
        default:
            break;
        }
    }
};
int main(){
    task1 t;
    t();
    t();
    return 0;
}

instance_local A a(result_of_calculation); that is what i could get from such a keyword instead of making a smart pointer for a.

ezegoing
  • 526
  • 1
  • 4
  • 18
  • 7
    Why not just create a regular member variable? – NathanOliver Oct 31 '19 at 18:38
  • Consider non-blocking function wrapper, where certain blocks are executed on each function call. So that function shall be called repeatedly until it finishes or succeeds. If you do like this you might have a class instance only being constructable after e.g. block 2. So the workaround now is making smart pointer member variables, but that boilerplate code adds up. Ill include an example if needed. – ezegoing Oct 31 '19 at 18:44
  • 2
    Yeah I think you'd need to give an example. – Lightness Races in Orbit Oct 31 '19 at 18:45
  • @ezegoing An example would most definitely help. – NathanOliver Oct 31 '19 at 18:45
  • 1
    I hope not!! Can you imagine trying to maintain code when reading the class definition can't be be relied on for determining the definition of the class because part of it could be buried in some function implementation? – Avi Berger Oct 31 '19 at 18:46
  • You cannot add or remove member variables of a class at run time. – Jesper Juhl Oct 31 '19 at 19:07
  • Sry for taking me so long with the example and sry for this silly example. But that is the case I was thinking of that such a keyword may come useful. – ezegoing Oct 31 '19 at 19:35
  • 2
    @ezegoing you're describing a coroutine! They are in C++20. Currently, it's implemented as a dynamically allocated object which his layout can be determined by the compiler backend for optimisation. The way to emulate that in C++17 or older is to implemente a state machine (your switch) + a state. The state would be better off as a member – Guillaume Racicot Oct 31 '19 at 19:37
  • @GuillaumeRacicot Oh man I feel so bad now. All these days and hours wasting into this and you tell me now there is coroutines in c++20... Guess I'm better off switching to coroutines then instead of reinventing the wheel. Thank you very much! – ezegoing Oct 31 '19 at 19:46
  • Also time travelling ;) – Lightness Races in Orbit Oct 31 '19 at 23:43
  • And, great example of why context matters! – Lightness Races in Orbit Oct 31 '19 at 23:44

3 Answers3

1

There is not. The compiler needs to know the structure of the class without compiling all the method implementations. If you could slip instance_local int foo into a method body, that would make the size of the data structure 4 bytes larger.

On a more principled level, it's not good to hide data. The equivalent feature for global variables that you might be thinking of, static local variables, is a carryover from C that is widely considered to be an anti-pattern: Why are static variables considered evil?

parktomatomi
  • 3,851
  • 1
  • 14
  • 18
1

You're describing a coroutine. Here a rough draft of what it could look like (I'm not an expert in coroutine)

auto task1() -> some_awaitable_type {
    result_of_calculation = 5;
    A a(result_of_calculation);

    co_yield;

    a.dosomethinguseful();
}

This could be called like this:

some_awaitable_type maybe_do_something = task1();
// calculation done here

// dosomethinguseful called here
co_await maybe_do_something();
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
0

Not directly, no.

You could define a:

static std::map<test_t*, int> is;

…where the first part of each element is a this pointer.

But, why?

Make a member variable.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055