Using repl, it appears to be completely legal, however is there any undefined behavior/compiler-specific issues at play here?
https://repl.it/repls/ContentPutridMacro
googling for the 'this' usage inside of a header doesn't seem to turn up anything useful. Here is a copy of my example if the repl link doesn't work.
Multiplier.h :
#include <functional>
class Multiplier
{
public:
Multiplier(int i);
void multiplyBy(int j);
std::function<void()> multiplyBy100 = std::bind(&Multiplier::multiplyBy, this, 100);
private:
int priv;
};
Multiplier.cpp:
#include <stdio.h>
#include "Multiplier.h"
Multiplier::Multiplier(int i) : priv(i)
{
// empty
}
void Multiplier::multiplyBy(int j)
{
printf("value = %d\n", priv * j);
}
How I'm understanding this currently is that when you create an instance of Multiplier, say
Multiplier m(25);
It will create a Multiplier object and put all its member variables on to the stack, one of them being
std::function<void()> multiplyBy100
meaning that instance can know what value 'this' should be pointing to. Maybe I'm overthinking this but I just have never seen something comparable before.
Thank you for the help!