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 9222745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:54:25+00:00 2026-06-18T03:54:25+00:00

Is declval<T>() just a replacement for the old trick of (*(T*)NULL) to get an

  • 0

Is declval<T>() just a replacement for the old trick of (*(T*)NULL) to get an instance of T in a decltype without needing to worry about T’s constructor?

Here is some sample code:

struct A {};

struct B {
    A a;
};

typedef decltype((*(B*)nullptr).a) T1;
typedef decltype(declval<B>().a) T2;

cout << "is_same: " << is_same<T1, T2>::value << endl;

which prints 1 because T1 and T2 are the same type.

If declval is more than a replacement, what are the differences and where is it useful?

  • 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-18T03:54:26+00:00Added an answer on June 18, 2026 at 3:54 am

    declval() has the advantage that if it is used in an evaluated context (i.e., odr-used) then the program is ill-formed (20.2.4p2), and a diagnostic is required to be issued (per 1.4p1). Typically this is enforced through a static_assert in the library:

    c++/4.7/type_traits: In instantiation of '[...] std::declval() [...]':
    source.cpp:3:22:   required from here
    c++/4.7/type_traits:1776:7: error: static assertion failed: declval() must not be used!
    

    declval also works on reference types:

    using S = int &;
    using T = decltype(std::declval<S>());
    using U = decltype(*(S *)nullptr);  // fails
    

    Where the type is not a reference type, declval will give an rvalue type where nullptr gives an lvalue.

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

Sidebar

Related Questions

Why can't a class have a decltype in the inheritance list? For instance, I
I'm going out on a limb here, assuming that it is possible but I'm
Stumped on this query, it searches an important table which contains about 213k rows.
Just to see how it worked, I looked at the libstdc++ implementation of std::common_type
I have some code: Class A{ //...A has a move ctor here. }; unordered_map<int,
I am trying to use decltype inside a template class as follows: #include <functional>
§20.2.4 [declval] template <class T> typename add_rvalue_reference<T>::type declval() noexcept; // as unevaluated operand Why
Below is one of the specializations of std::common_type . template <class T, class U>
I've a function that works well for me: template <typename __container, typename __callback =
I have 2 database fields `decval` decimal(5,2) `intval` int(3) I have 2 pdo queries

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.