SelfType in Macro.

some days ago, I write a macro that needs '(*this)' type.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <type_traits>
#define GenerateSelfType() using SelfType = std::remove_reference_t<decltype(*this)>;
struct A
{
	GenerateSelfType()
};
int main()
{}

The above code compiled fail. The main reason is that 'this' is not in the right place.

I need the macro is independent of the specific class, such as 'A'.

So I write a substitute.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <type_traits>

template<class Self>
struct MemberFunctionParse;

template<class Result, class Self, class... Args>
struct MemberFunctionParse<Result(Self::*)(Args...)>
{
	using SelfType = Self;
};

#define GenerateSelfType()\
private:\
auto SelfTypeNeverUseManually()->std::remove_reference_t<decltype(*this)>;\
public:\
using SelfType = MemberFunctionParse<decltype(&SelfTypeNeverUseManually)>::SelfType;
struct A
{
	GenerateSelfType()
};
struct B : public A
{
	GenerateSelfType()
};
int main()
{
	static_assert(std::is_same<B, B::SelfType>::value,"bad");
	static_assert(std::is_same<A, A::SelfType>::value,"bad");
}


g++ needs '-fpermissive'.


Who can privode more simple method?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# include <iostream>
# include <type_traits>
# include <iomanip>

struct A{};

int main()
{
    std::cout << std::boolalpha << std::is_same <A, A&>() << "\n"; //false
    std::cout << std::is_same <A, std::remove_reference<A&>::type>() << "\n"; //true
    std::cout << std::is_same <A, std::remove_reference<A&&>::type>() << "\n"; //true

    std::cout << "\n\n";

    std::cout << std::is_same <A, A&> () << "\n"; //false
    std::cout << std::is_same <A, std::remove_reference_t<A&>> () << "\n"; //true
    std::cout << std::is_same <A, std::remove_reference_t<A&&>> () << "\n"; //true

}
Last edited on
Are you answering my questions?
Are you answering my questions?

yeah, I was hoping I was but since you're asking probably not? In that case, do clarify and while you do so permit me another interpretation of the problem which I think is quite interesting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# include <iostream>
# include <type_traits>
# include <iomanip>

struct A{A* a;};
struct B{};

template <typename T> struct has_own_pointer : std::false_type{};

template <> struct has_own_pointer <A> : std::true_type{};

int main()
{
    std::cout << std::boolalpha << has_own_pointer<B> :: value << "\n";
    std::cout << has_own_pointer<A> :: value << "\n";
}

Here I'm assuming your SelfType is akin to a pointer to the same type (obviously it can't be an object of the same type since that'd result in an incomplete type)
I want to get the current type of the current specific type in a macro.

the macro is independent of the specific type.

Topic archived. No new replies allowed.