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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:13:15+00:00 2026-05-27T07:13:15+00:00

Consider this template: template< typename T, typename RefT = T& > class foo {

  • 0

Consider this template:

template< typename T, typename RefT = T& >
class foo
{
    typedef const RefT const_ref_t;
    typedef const T&   another_const_ref_t;

    //...

};

I would assume that the types const_ref_t and another_const_ref_t would be equivalent. Both are const T&‘s. Yet they are not. Alas, the following demonstration of their not-equivalence is fairly elaborate. It hinges on using dynamic_cast<> to check the type of of another class.

class abstractBase
{
public: virtual ~abstractBase() {}
};

template< typename T >
class otherClass : public abstractBase
{
};

template< typename T, typename RefT = T& >
class foo
{
    typedef const RefT const_ref_t;
    typedef const T&   another_const_ref_t;

public:
    void discover( abstractBase* p )
    {
        otherClass< const_ref_t >* a = 
            dynamic_cast< otherClass< const_ref_t >* >( p );
        otherClass< another_const_ref_t >* b = 
            dynamic_cast< otherClass< another_const_ref_t >* >( p );

        assert( a );    // Fails
        assert( b );    // Succeeds
    }
};

void fn()
{
    abstractBase* p = new otherClass< const int& >();
    foo< int > f;
    f.discover( p );   // Assertion on 'a' fails.
}

Sorry this is so complex, but it’s a simplified version of the situation where I discovered the question.

The question, then, is this. This code treats const int&, foo< int >::const_ref_t, and foo< int >::another_const_ref_t as equivalent, which seems reasonable given the typedefs. Yet dynamic_cast<> only treats foo< int >::another_const_ref_t as equivalent to const int&. It will return null in the other (foo< int >::const_ref_t) case.

Why?

  • 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-05-27T07:13:16+00:00Added an answer on May 27, 2026 at 7:13 am

    Consider this:

    typedef Foo T;
    typedef T & TRef;
    typedef T const & TCRef;
    

    Now TRef is the same as Foo &, and TCRef is the same as const Foo &.

    However, const TRef is the same as const (TRef) = const (Foo &), not (const Foo)&. But reference types are always constant, so the additional const doesn’t add anything.

    If you prefer the comparison with pointers: T& is essentially like T * const, so TRef const is like (T * const) const, which just collapses to T * const.

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

Sidebar

Related Questions

Consider this simple class: template<class T> class Foo{ public: Foo(T const& val) : _val(val)
Consider something like... template<typename T> class Vector { ... bool operator==( const Vector<float> &rhs
Consider this overly simple test: class foo { public: foo(int i); template< typename T
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
Consider this code: template <typename T> class String { public: ... String(T* initStr) {
Consider the following code: class MyClass { template <typename Datatype> friend MyClass& operator<<(MyClass& MyClassReference,
Consider: template <typename T> class Base { public: static const bool ZEROFILL = true;
Please consider the following code. struct foo { }; template<typename T> class test {
Consider this one template<typename T> struct A { enum class X { V =
Consider the following: template <typename T> class testString { public: typedef T* iterator; void

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.