ARTICLE AD BOX
I have this code
#include <iostream> template <typename... Ts> struct LOG { LOG( const char * charPtr, Ts &&... LogArguments // , const auto & member = this->m_member // error: ‘this’ may not be used in this context ) { std::cout << "In LOG" << std::endl; // do more... } }; template <typename... Ts> LOG( const char *, Ts &&... ) -> LOG<Ts...>; class Faewe { public: int m_member {73}; auto doSomething() -> void { std::cout << "m_member=" << this->m_member; // Works LOG("hello", 73); } }; int main() { Faewe faewe; faewe.doSomething(); return 0; }I would like to access the member variable m_member from the class in which the template function LOG() is called. If m_member does not exist in the class where LOG() is called, then the compiler throws an error - this is the expected behaviour. If m_member does exist then its value should be passed to the template as a default parameter value [in this example to template function parameter member].
In the above code the compiler throws error: ‘this’ may not be used in this context.
My question: How can I access any member variable in the encapsulating class?
NOTE: this->m_member does not have to be provided in LOG( ..., this->m_member) due to simplicity. It should be accessed only in the template, so there is only "one" place where it's accessed instead of hundred times in each call of a LOG() call.
5,3533 gold badges50 silver badges87 bronze badges
4
