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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:10:53+00:00 2026-06-01T16:10:53+00:00

Essentially, I’m curious if you can use C++11 templates to make it so a

  • 0

Essentially, I’m curious if you can use C++11 templates to make it so a templated function can detect an iterator’s level of indirection and compile the function differently depending on that. For example, here is some code that won’t compile:

#include <vector>
#include <list>
#include <type_traits>
#include <iostream>

struct MyStruct
{
  int value;
  MyStruct(int value = 42) : value(value) { }
  const int& getInt() const { return value; }
};

typedef std::list<MyStruct> StructList;
typedef std::vector<const MyStruct*> StructPtrVector;

template <typename ITER_TYPE>
const int& getIteratorInt(ITER_TYPE iter)
{
  if (std::is_pointer<decltype(*iter)>::value)
    return (*iter)->getInt(); // Won't compile -> MyStruct has no operator-> defined
  return iter->getInt(); // Won't compile -> MyStruct* has the wrong level of indirection
}

template <typename LIST_TYPE>
void PrintInts(const LIST_TYPE& intList)
{
  for (auto iter = intList.begin(); iter != intList.end(); ++iter)
    std::cout << getIteratorInt(iter) << std::endl;
}

int main(void)
{
  StructList structList;
  StructPtrVector structPtrs;
  int values[5] = { 1, 4, 6, 4, 1 };
  for (unsigned i = 0; i < 5; ++i)
  {
    structList.push_back(values[i]);
    structPtrs.push_back(&structList.back());
  }
  PrintInts(structList);
  PrintInts(structPtrs);

  return 0;
}

The obvious situation is when you have a list of objects, and then a different kind of list of pointers to objects. And, what you want to do is the same to both lists, by treating them both as lists of objects.

The above code won’t compile, because it is doing a logical check that should be done at compile-time. I don’t know if there is a way to do this with preprocessor macros. I tried a simple #if std::is_pointer<decltype(*iter)>::value == true, but the compiler seems to always consider it false. (I’ve never tried preprocessor macros much before, but that is clearly not the proper way.)

Any idea if it’s even possible?

  • 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-01T16:10:55+00:00Added an answer on June 1, 2026 at 4:10 pm

    When you want to select between two implementations depending on a metafunction such as is_pointer, use std::enable_if, which works on the principle of SFINAE.

    template <typename ITER_TYPE>
    auto getIteratorInt(ITER_TYPE iter) ->
    typename std::enable_if< std::is_pointer< 
            typename std::iterator_traits< ITER_TYPE >::value_type >::value,
        const int& >::type
    {
      return (*iter)->getInt();
    }
    
    template <typename ITER_TYPE>
    auto getIteratorInt(ITER_TYPE iter) ->
    typename std::enable_if< ! std::is_pointer< 
            typename std::iterator_traits< ITER_TYPE >::value_type >::value,
        const int& >::type
    {
      return iter->getInt();
    }
    

    This way, template instantiation only sees lines of code that are valid for the given template arguments.

    I’ve just applied this fix to your code mechanistically… I’m not advocating the use of multiple indirection or suggesting that the fixed implementation is robust.

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

Sidebar

Related Questions

Essentially, I'm looking for a 1D bar-code scanner that I can program, either through
Essentially I want a border-less, black window which I can set the location and
Essentially, is there any way to make this code legal? main = print .
Essentially I'm wondering if the following can be done in Ruby. So for example:
Essentially what I want to do is impliment a class that can contain a
Essentially here I want the modal form sheet to be transparent so I can
Essentially, what I'm looking for is a function that would allow me to do
Essentially I am looking for a PHP function or equation that will output the
Essentially, I need to know how to make a UIWebView erase everything when you
Essentially I want to know if a specific XSD schema can be replaced by

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.