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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:31:50+00:00 2026-05-25T09:31:50+00:00

I was merily experimenting with the new trailing return types, where I hit a

  • 0

I was merily experimenting with the new trailing return types, where I hit a problem with this (simplified) code

#include <list>

class MyContainer{
  std::list<int> ints;

  auto begin( ) -> decltype(ints.begin())
  {
    return ints.begin();
  }

  auto begin( ) const -> decltype(ints.begin())
  {
    return ints.begin();
  }
};

Ignore the fact of how pointless this code is. The important part is the compiler error generated when using GCC 4.6.1 (with -std=c++0x flag):

In member function 'std::list<int>::iterator MyContainer::begin() const':
error: could not convert '((const MyContainer*)this)->MyContainer::ints.std::list<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>, std::list<_Tp, _Alloc>::const_iterator = std::_List_const_iterator<int>]()' from 'std::list<int>::const_iterator {aka std::_List_const_iterator<int>}' to 'std::list<int>::iterator {aka std::_List_iterator<int>}'

In case you’re not of fan of error involving templates, the short story is that in the body of the const version of MyContainer::begin, the expression ints.begin() returns a value of type std::list<int>::const_iterator (since ints is const in such a context). However, decltype(ints.begin()) produces the type std::list<int>::iterator, i.e. decltype ignores the const qualifier of the begin method when deciding the type of the expression. Unsurprisingly, a conflict in types is the result.

This seems to me to be a bug in the GCC compiler. It would only make sense for decltype to honor the const qualifier and produce the const_iterator type. Can anyone confirm or deny (maybe even explain) this? Maybe I am overlooking something in the mechanics of decltype, but this looks like a pretty straightforward scenario.

Note: as far as I can tell, the same behaviour holds not only for std::list<int>, but for any type with member functions overloaded on const-ness which return incompatible types.

  • 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-25T09:31:50+00:00Added an answer on May 25, 2026 at 9:31 am

    You are correct, this is a bug. According to N3291, section 5.1.1, paragraph 3:

    If a declaration declares a member function or member function template of a class X, the expression this is a prvalue of type “pointer to cv-qualifier-seq X” between the optional cv-qualifer-seq and the end of the function-definition, member-declarator, or declarator. It shall not appear before the optional cv-qualifier-seq and it shall not appear within the declaration of a static member function (although its type and value category are defined within a static member function as they are within a non-static member function). [Note: this is because declaration matching does not occur until the complete declarator is known. —end note ] Unlike the object expression in other contexts, *this is not required to be of complete type for purposes of class member access (5.2.5) outside the member function body. [Note: only class members declared prior to the declaration are visible. —end note ]

    But this was a recent change between the last working draft and N3291. So GCC was right less than 6 months ago; that’s the danger of writing code to a moving specification.

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

Sidebar

Related Questions

I am implementing my own linked list in Java. The node class merely has
I have this problem when using merely load() was sufficient, then of course I
The suggested query to list ENUM types is great. But, it merely lists of
My knowledge of matlab is merely on a need to know basis, so this
this is not a primary key, but merely used as a marker in existing
I'm in the process of experimenting a bit with the twisted libraries for IRC
I am going to paste a code here and had a question regarding that
Suppose I've an empty list L. Currently if I run L.front(), it will merrily
I have the following code which throws an exception (detail in code comments below).
MSDN states of the property TypeId that: As implemented, this identifier is merely the

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.