Как получить доступ к параметрам шаблона? ⇐ C++
-
Anonymous
Как получить доступ к параметрам шаблона?
If I need to know template parameters in some other template which uses it, how can I access them?
Is the way below with the constexpr static size_t data_size the recommended approach or there is some syntax which could allow to get access from the data_user_typed to _data_size in ExtendableIndex without such constexpr static?
I know that is comes for free in term of the generated code, but, anyway, is it the recommended approach or this could be done in more straightforward way in the language?
Here is the demo
#include using data_type = int; template struct ExtendableIndex { constexpr static std::size_t data_size = _data_size; std::size_t index; std::array data; }; template struct ExtendableIndex { constexpr static std::size_t data_size = 0; std::size_t index; }; constexpr std::size_t cache_length = 0; // Can be set to any cache size including 0 using DefaultIndex = ExtendableIndex; template ExtendableIndex data_user() { ExtendableIndex index{}; if constexpr (_data_size > 0) { auto value = index.data[0]; } return index; } template T data_user_typed() { T index{}; if constexpr (T::data_size > 0) { auto value = index.data[0]; } return index; } int main() { auto index_one = data_user(); // Works, but requires passing length, not a type auto index_two = data_user(); // Seems to be better auto index_three = data_user_typed(); // Good, but still requires constexpr static data_size }
Источник: https://stackoverflow.com/questions/780 ... parameters
If I need to know template parameters in some other template which uses it, how can I access them?
Is the way below with the constexpr static size_t data_size the recommended approach or there is some syntax which could allow to get access from the data_user_typed to _data_size in ExtendableIndex without such constexpr static?
I know that is comes for free in term of the generated code, but, anyway, is it the recommended approach or this could be done in more straightforward way in the language?
Here is the demo
#include using data_type = int; template struct ExtendableIndex { constexpr static std::size_t data_size = _data_size; std::size_t index; std::array data; }; template struct ExtendableIndex { constexpr static std::size_t data_size = 0; std::size_t index; }; constexpr std::size_t cache_length = 0; // Can be set to any cache size including 0 using DefaultIndex = ExtendableIndex; template ExtendableIndex data_user() { ExtendableIndex index{}; if constexpr (_data_size > 0) { auto value = index.data[0]; } return index; } template T data_user_typed() { T index{}; if constexpr (T::data_size > 0) { auto value = index.data[0]; } return index; } int main() { auto index_one = data_user(); // Works, but requires passing length, not a type auto index_two = data_user(); // Seems to be better auto index_three = data_user_typed(); // Good, but still requires constexpr static data_size }
Источник: https://stackoverflow.com/questions/780 ... parameters
Мобильная версия