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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:46:59+00:00 2026-06-14T18:46:59+00:00

Today, I was trying to perform a type switch on whether some class has

  • 0

Today, I was trying to perform a type switch on whether some class has the nested type reverse_iterator or not. I found on these fora some working solution, which is the following one:

template<typename T>
struct is_reverse_iterable
{
    using yes   = uint8_t;
    using no    = uint16_t;

    template<typename U>
    static yes& test(typename U::reverse_iterator*);

    template<typename>
    static no& test(...);

    static constexpr bool value = sizeof(test<T>(0)) == sizeof(yes);
};

This class works just fine if I simply check the condition from the main, however, I also wrote that little function, which cause some problems.

template<typename T>
void foo(T&& iter)
{
    std::cout << typeid(T).name() << std::endl;
    std::cout << is_reverse_iterable<T>::value << std::endl;
}

Here is the main that causes me some problems:

int main()
{
    using namespace std;

    vector<int> v;

    cout << typeid(decltype(v)).name() << endl;
    cout << is_reverse_iterable<decltype(v)>::value << endl;

    foo(v);

    return 0;
}

As std::vector<int> contains the nested type name reverse_iterator, one would think – or at least, I would think – that is_reverse_iterable<vector<int>>::value would return true wherever I put it. But it’s not the case. Here is the result of the main above:

St6vectorIiSaIiEE
1
St6vectorIiSaIiEE
0

When called from the main, the struct is_reverse_iterable recognized the name reverse_iterator in vector<int>, but it did not do so when called from foo. Actually, I have no idea why, and I would like someone to please explain to me what the problem is 🙂

P.S. : I use MinGW g++ 4.7.1 to compile, with the option -std=c++11.

  • 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-14T18:47:01+00:00Added an answer on June 14, 2026 at 6:47 pm

    The problem is that when you call foo(v), it is deduced that T is of type std::vector<int>& (an lvalue-reference), so typename T::reverse_iterator will not compile. You can easily check it yourself:

    template<typename T>
    void foo(T&& iter)
    {
        std::cout << typeid(T).name() << std::endl;
        std::cout << is_reverse_iterable<T>::value << std::endl;
    
        typename T::reverse_iterator t;  // <-- add this line to see what's wrong
    }
    

    Yields:

    3.cpp: In instantiation of ‘void foo(T&&) [with T = std::vector<int>&]’:
    3.cpp:40:10:   required from here
    3.cpp:27:34: error: ‘std::vector<int>&’ is not a class, struct, or union type
    

    The solution is simple: remove the reference before you start the SFINAE, e.g.

    static constexpr bool value = sizeof(test<typename std::decay<T>::type>(0))
                                    == sizeof(yes);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was today trying to figure out on working with WebService and found many
I've search all over today trying to find how to do this, and not
Today I am trying to remove some bytes from an EXE file. Inside the
Today I wanted to perform this task, and I run across some issues in
Actually i had been practicing some of the java programs today(trying to learn it
I was having a horrible time today trying to get a query to perform
I got an error today while trying to do some formatting to existing code.
I ran into a problem today trying to override an implementation of an interface
I ran across the caret operator in python today and trying it out, I
I came across Side-by-side Assemblies for the first time today while trying to install

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.