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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:22:16+00:00 2026-05-14T04:22:16+00:00

Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename

  • 0

Imagine I have a template function like this:

template<typename Iterator>
void myfunc(Iterator a, typename Iterator::value_type b)
{ ... }

Is there a way to implement the same thing by declare a typedef for Iterator::valuetype that I can use in the function signature? For example, I’d prefer to be able to do something like this:

template<
    typename Iterator,
    typedef Iterator::value_type type>
void myfunc(Iterator a, type b)
{ ... }

Thus far, I’ve resorted to using default template arguments and Boost concept checking to ensure the default is always used:

template<
    typename Iterator,
    typename type = typename Iterator::value_type >
void myfunc(Iterator a, type b)
{
     BOOST_STATIC_ASSERT((
         boost::is_same<
             typename Iterator::value_type, 
             type
         >::value
     ));
     ...
}

…but it would be nice if there was support in the language for this type of thing.

Edit

I probably should have used a class instead of a function, since default arguments aren’t standard for functions.

template<
    typename T,
    typename V = typename T::value_type>
class A : public B<T, V>  
{
    BOOST_STATIC_ASSERT((boost::is_same<typename T::value_Type, V>::type));
};
  • 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-14T04:22:16+00:00Added an answer on May 14, 2026 at 4:22 am

    You are looking for a templated typedef to be used inside a templated function definition. I don’t think you can do that…

    You can have a templated class with a static function & typedefs… But using it gets ugly:

    template<typename Iterator>
    class arbitraryname
    {
    public:
      typedef typename Iterator::value_type  value;
    
      static void myfunc( Iterator a, value b )
      {
        value c = b;
        cout << "Test" << c << endl;    
      }
    };
    
    struct Foo
    {
      typedef int value_type;
    };
    
    int main()
    {
      Foo f;
      myfunc<Foo>(f,2); // Old way.
      arbitraryname<Foo>::myfunc(f,3); // With templated class.
    }
    

    Personally, in this case, I’d go with a #define…

    #define VALUE_TYPE  typename Iterator::value_type
    template<typename Iterator>
    void myfunc(Iterator a, VALUE_TYPE b)
    #undef VALUE_TYPE
    {
      typedef typename Iterator::value_type  bar;
      bar z = b;
      cout << "Test" << z << endl;
    }
    

    Sure #define‘s are ugly and sinful. But so is code that’s painfully obtuse to read…

    p.s. Just to be safe, you might want to add:

    #ifdef  VALUE_TYPE
    #error "VALUE_TYPE already defined!"
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a template function like this: template<typename T> void foo(T a)
Imagine you have a file a.h #include <iostream> template<typename T> struct A{ int magic;
Imagine I have the following free function and functor: void myFreeFunction(void) { cout <<
I have an ajax request that looks like this, $(#frmProducts).submit(function(){ var dataSet = $(#frmProducts).serialize();
I want to do something like this: template<typename CType, unsigned int targetDimensions> struct GeneratePointerType
Here's a riddle. Imagine I have the following C++ function: template<uint8_t MASK> uint8_t Foo(uint8_t
Ok, let's imagine I have an object like this: public class User { public
Imagine you have two views with code like the following: controller_a/a.html.erb <%= content_tag(:div) do
Imagine I have this table: Column A | Column B | Column C ------------------------------
Imagine that in one data template, I have a textBox, and another data template,

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.