C++ 一般地获取结构';在它的顶层有自己的类型

C++ 一般地获取结构';在它的顶层有自己的类型,c++,types,C++,Types,是否有一种方法可以在所述结构声明的顶层一般获取结构的类型,而无需参考结构本身的实际名称 例如: #include <type_traits> struct example { using attempt1 = example; // <- obvious, but not what I want using attempt2 = decltype(*this); // <- error using attempt3 = std::remove_p

是否有一种方法可以在所述
结构
声明的顶层一般获取
结构的类型,而无需参考
结构
本身的实际名称

例如:

#include <type_traits>

struct example {
    using attempt1 = example; // <- obvious, but not what I want
    using attempt2 = decltype(*this); // <- error
    using attempt3 = std::remove_pointer<decltype(this)>::type; // <- error
};
#包括
结构示例{

使用attempt1=example;//朋友注入来拯救

namespace impl
{
名称空间
{
模板
结构标记{using type=T;};
模板
结构adl_标签
{
friend constexpr自动adl_功能(adl_标签);
};
模板
结构adl_编写器
{
friend constexpr auto adl_func(adl_标记){return tag{};}
};
void adl_func()=删除;
模板
结构adl_读取器
{
使用type=typename decltype(adl_func(adl_tag{}))::type;
};
}
};
#定义自己的类型\
[[maybe_unused]]void self_type_helper_dummy()\
{ \
(无效):impl::adl_writer{}\
} \
[[maybe_unused]]static auto self_type_helper()\
{ \
return::impl::adl_reader{}\
}
#定义SELF\u TYPE decltype(SELF\u TYPE\u helper())::TYPE
然后

#include <iostream>
#include <typeinfo>

struct A
{
    KNOWS_SELF_TYPE

    static void foo()
    {
        std::cout << typeid(SELF_TYPE).name() << '\n';
    }
};

int main()
{
    A::foo(); // Prints `1A` (mangled `A`).
}
#包括
#包括
结构A
{
知道自己的类型
静态void foo()
{

std::cout我能找到的最接近的东西是这个,看看它是否有助于你或这个用
class
而不是
struct
来搜索这个单词,以获得更好的结果也许这也可以做一个
模板类SelfTypeHolder{using SelfType=T;};
并像
类SomeClass:public SelfTypeHolder{}
,就像奇怪的循环模板一样?不幸的是,可用的解决方案似乎只有在结构的非静态函数中使用时才起作用。谢谢大家,我回家后会稍微检查一下上述注释中的链接,然后此注释将自毁。
namespace impl
{
    namespace
    {
        template <typename T>
        struct tag { using type = T; };

        template <int L>
        struct adl_tag
        {
            friend constexpr auto adl_func(adl_tag<L>);
        };

        template <int L, typename T>
        struct adl_writer
        {
            friend constexpr auto adl_func(adl_tag<L>) {return tag<T>{};}
        };

        void adl_func() = delete;

        template <int L>
        struct adl_reader
        {
            using type = typename decltype(adl_func(adl_tag<L>{}))::type;
        };
    }
};

#define KNOWS_SELF_TYPE \
    [[maybe_unused]] void self_type_helper_dummy() \
    { \
        (void)::impl::adl_writer<__LINE__, std::remove_cvref_t<decltype(*this)>>{}; \
    } \
    [[maybe_unused]] static auto self_type_helper() \
    { \
        return ::impl::adl_reader<__LINE__>{}; \
    }

#define SELF_TYPE decltype(self_type_helper())::type
#include <iostream>
#include <typeinfo>

struct A
{
    KNOWS_SELF_TYPE

    static void foo()
    {
        std::cout << typeid(SELF_TYPE).name() << '\n';
    }
};

int main()
{
    A::foo(); // Prints `1A` (mangled `A`).
}