Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7958383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:24:52+00:00 2026-06-04T04:24:52+00:00

In my code, I need to test if a type given to a template

  • 0

In my code, I need to test if a type given to a template is a pointer — be it smart or not. According to boost, there is no reliable and generic way to do that (see here) — or is there?

So far, I check for the following:

  • A: Can T be converted to void*?
  • B: Does T have a get() method?
  • C: Does T have a type called element_type?
  • D: Does get() return an element_type*?

If (A || B && C && D), then I conclude that my type must be some kind of pointer.

Here’s the template:

template <typename T>
class is_pointer_type
{
    typedef struct { char array[1]; } yes;
    typedef struct { char array[2]; } no;

    template <typename C> static yes test_g(decltype(&C::get));
    template <typename C> static no  test_g(...);

    template <typename C> static yes test_e(typename C::element_type*);
    template <typename C> static no  test_e(...);

    enum {
        has_get          = sizeof(test_g<T>(0)) == sizeof(yes),
        has_element_type = sizeof(test_e<T>(0)) == sizeof(yes)
    };

    template <typename Q, bool OK = false>
    struct get { struct type {}; };

    template <typename Q>
    struct get<Q, true>
    {
        typedef decltype(((Q*)nullptr)->get()) type;
    };

    template <typename Q, bool OK = false>
    struct ptr { struct type {}; };

    template <typename Q>
    struct ptr<Q, true>
    {
        typedef typename Q::element_type* type;
    };

public:
    enum {
        types_ok = std::is_same<
                           typename get<T, has_get>::type,
                           typename ptr<T, has_element_type>::type
                   >::value,
        value    = std::is_convertible<T, void*>::value || types_ok
    };
};

So far, it seems to work out ok. But is there something wrong with this reasoning? Should I be prepared for unpleasant surprises? What about const / volatile?

Update (Motivation):

In the comments you ask for my motivation and they are right, I owe you one. The use case is a Lua – C++ binding library: when exposing a class instance to Lua with template <typename T> push_value(T value), I need to deduce the underlying type U in any combination of T = U const/volatile/*/& and T = some_pointer<U>. I need to know if the underlying class U has been registered already with the binder.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:24:53+00:00Added an answer on June 4, 2026 at 4:24 am

    It’s easy to check if a type is a pointer, either using boost or defining a custom template with a specialization like

    template <typename C> static no test_pointer(C);
    template <typename C> static yes test_pointer(C*);
    

    But you can stick with the void* solution if you like it more.

    To check smart pointers, I suggest checking for appropriate operators instead. I think a type can be considered to be a smart pointer only if it has both operator* and operator-> defined. So you should check for

    template <typename C> static yes test_deref(decltype(&C::operator*));
    template <typename C> static no test_deref(...);
    template <typename C> static yes test_arrow(decltype(&C::operator->));
    template <typename C> static no test_arrow(...);
    

    and require both results to be ‘yes’. So, the final formula can be calculated as “normal pointer || (has operator* && has operator->)”.

    However, it’s a solution only for smart pointers. If you also want to pass types other than smart pointers (other wrappers, collections, etc) to Lua than it’s a whole different story and I don’t dare to propose a solution for that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code in my error handler I need to test against a
I'm looking to test some code I've written and to do so I need
Following code works ok, except that it not zoom to the given points. If
I need to create a variable of type resource, which is not a stream.
I need files to be downloaded to /tmp/cron_test/. My wget code is wget --random-wait
Both my Rails model and controller code need to write files to the file
In my code I need to compare string letters but my problem is that
I have a code that need to be translated from C to Cpp, and
In the following code I need to print output from the variables $stout ,
The C code i need to convert into Java is: typedef struct { short

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.