Информация об изменениях

Сообщение Re[2]: Аттрибуты для полей класса от 14.04.2025 12:46

Изменено 14.04.2025 12:50 rg45

Re[2]: Аттрибуты для полей класса
Здравствуйте, kov_serg, Вы писали:

_>Например так: https://godbolt.org/z/c6YWGc58d

_>
_>struct A {
_>    string s;
_>    int i;
_>};

_>template<auto>struct Info;
_>template<>struct Info<&A::s> { enum { id=1 }; static const char* name() { return "string"; } };
_>template<>struct Info<&A::i> { enum { id=2 }; static const char* name() { return "integer"; } };
_>


Сдаётся мне, это неплохой повод, чтобы использовать самый честный компайл-тайм счётчик на свете
Автор: rg45
Дата: 27.11.24
:

https://coliru.stacked-crooked.com/a/79b32183d6288b09

struct A {
    std::string s;
    int i;
};

template<auto>struct Info;
template<>struct Info<&A::s> { enum { id = compile_time::ct_tick<A> }; static const char* name() { return "string"; } };
template<>struct Info<&A::i> { enum { id = compile_time::ct_tick<A> }; static const char* name() { return "integer"; } };

int main() {
    std::cout << Info<&A::s>::id << ", " << Info<&A::s>::name() << std::endl;
    std::cout << Info<&A::i>::id << ", " << Info<&A::i>::name() << std::endl;
}
Re[2]: Аттрибуты для полей класса
Здравствуйте, kov_serg, Вы писали:

_>Например так: https://godbolt.org/z/c6YWGc58d

_>
_>struct A {
_>    string s;
_>    int i;
_>};

_>template<auto>struct Info;
_>template<>struct Info<&A::s> { enum { id=1 }; static const char* name() { return "string"; } };
_>template<>struct Info<&A::i> { enum { id=2 }; static const char* name() { return "integer"; } };
_>


Сдаётся мне, это неплохой повод, чтобы использовать самый честный компайл-тайм счётчик на свете
Автор: rg45
Дата: 27.11.24
. Чтобы исключить возможные ошибки при ручном проставлении идентификаторов:

https://coliru.stacked-crooked.com/a/79b32183d6288b09

struct A {
    std::string s;
    int i;
};

template<auto>struct Info;
template<>struct Info<&A::s> { enum { id = compile_time::ct_tick<A> }; static const char* name() { return "string"; } };
template<>struct Info<&A::i> { enum { id = compile_time::ct_tick<A> }; static const char* name() { return "integer"; } };

int main() {
    std::cout << Info<&A::s>::id << ", " << Info<&A::s>::name() << std::endl;
    std::cout << Info<&A::i>::id << ", " << Info<&A::i>::name() << std::endl;
}