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

  • Home
  • SEARCH
  • 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 9168915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:44:43+00:00 2026-06-17T15:44:43+00:00

To be more specific, suppose I am writing template<class Pointer> class Foo and I

  • 0

To be more specific, suppose I am writing template<class Pointer> class Foo and I want to declare a typedef inside the class for the type that *p would have if p were of type Pointer.

In C++03, as far as I am aware, the only way to do this is with something like

typename std::iterator_traits<Pointer>::reference

The disadvantage of this method is that it won’t work if Pointer is some custom iterator type and the author forgot to extend std::iterator or otherwise define a std::iterator_traits specialization.

In C++11, my colleague suggested

decltype(*Pointer())

But this won’t work if Pointer is not default-constructible, so he amended this to

decltype(**(Pointer*)0)

I tried this, and it worked, but then I thought that it looked a bit iffy because it involves the dereference of a null pointer, and thus might not be standards-compliant.

Can we do better?

  • 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-17T15:44:45+00:00Added an answer on June 17, 2026 at 3:44 pm

    You’re right to be cautious about dereferencing a null pointer, but the fact is that it’s ok here! decltype does not evaluate its operand, so dereferencing a null pointer inside is perfectly valid.

    The proper solution, however, is std::declval, introduced inside <utility> in C++11:

    decltype(*std::declval<Pointer>())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am editing the question to make it more specific. I want to run
I'm often confused by CSS override rules: in general, I realize that more specific
I understand that in jQuery, it's advantageous to be more specific when using selectors
Suppose I want to create an abstract class in Python with some methods to
If I have a class hierarchy in which subclasses require use of more specific
In nunit, suppose we have more than one actual values for the specific function
Say I have a higher kinded type SuperMap[Key[_],Value[_]]`. Suppose now that I had something
I do have something more specific in mind, however: Each web service method needs
To be more specific I do not know what the exact values will be.
Surely an ordered set is a more-specific case of a set, so why does

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.