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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:49:37+00:00 2026-05-19T02:49:37+00:00

Is it possible to create template to the initialization like: template <typename C> typename

  • 0

Is it possible to create template to the initialization like:

template <typename C> typename C::value_type  fooFunction(C& c) {...};
std::vector<string> vec_instance;
fooFunction(cont<0>(vec_instance));
fooFunction(cont<1>(vec_instance));

In general i’m interested is it possible to specify template using integer (ie. 0) instead of true type name.
And how to achieve above?

  • 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-19T02:49:38+00:00Added an answer on May 19, 2026 at 2:49 am

    I’m not completely clear on what you’re asking, but the following snippet works for me:

    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    template <typename C>
    typename C::value_type  fooFunction(const C & c) { return 0; };
    /* note that fooFunction takes a ref-to-const, not a reference  */ 
    
    template<int N>
    struct cont
    {
    public:
       typedef int value_type;
       cont(vector<string> vec) {};
    };
    
    int main()
    {
    std::vector<string> vec_instance;
    fooFunction(cont<0>(vec_instance));
    fooFunction(cont<1>(vec_instance));
    }
    

    Two changes worth noting:

    1. An integer is not a type, so if cont is declared template <typename T>, what you have written will not work. template <int N> is the proper way to parameterize over an integral value, as templatetypedef mentioned.

    2. I’m not sure how cont<> is defined, but from your usage it must be an object you are constructing as a temporary. You will have trouble passing this temporary as a reference into fooFunction. Note that my example above passes C as reference-to-const instead.

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

Sidebar

Related Questions

Is it possible to create a template accepting types which implement certain interface? For
Is it possible to create a template function that takes a variable number of
I want to know if it's possible to create a template function and then
Is it possible to create a certificate template that uses SHA-2 (sha256, sha224, sha384,
Is it possible to create if sentences inside a jQuery tmpl template? <script id=template
is it possible in C++ to create an alias of template class (without specifying
is it possible to create a custom JSTL tag that accepts a non-string attribute?
I would like to know if it is possible to create an actual functor
I was wondering if it is possible to create a template for a view
I would like to create a style or template for rectangles. The properties are

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.