Шаблонная функция класса T: Как узнать, является ли T указателем?

Я думаю, что нет, по крайней мере, не, если Ваш источник оптимизирован и т.д. Однако существуют некоторые макросы для gdb, который может осмотреть контейнеры STL для Вас:

http://sourceware.org/ml/gdb/2008-02/msg00064.html

Однако я не использую это, таким образом, YMMV

6
задан Community 23 May 2017 в 12:23
поделиться

3 ответа

No need to specialize member function. In that answer used stand-alone structure. You're still free to use it in class member functions.

// stand-alone helper struct
template<typename T>
struct is_pointer { static const bool value = false; };    
template<typename T>
struct is_pointer<T*> { static const bool value = true; };

// your class
class Class{
public:
 template<typename T>
 void Fun(T& variable) {
     std::cout << "is it a pointer? " << is_pointer<T>::value << std::endl;
 }
};

On the other hand, you could overload function:

class Class {
public:
 template<typename T>
 void Fun(T& variable) {
     std::cout << "is it not a pointer! " << std::endl;
 }
 template<typename T>
 void Fun(T*& variable) {
     std::cout << "is it a pointer! " << std::endl;
 }
};
22
ответ дан 8 December 2019 в 02:46
поделиться

Have a look at Boost.TypeTraits, together with Boost EnableIf.

3
ответ дан 8 December 2019 в 02:46
поделиться

Take a look at boost::is_pointer.

8
ответ дан 8 December 2019 в 02:46
поделиться
Другие вопросы по тегам:

Похожие вопросы: