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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:54:50+00:00 2026-05-24T23:54:50+00:00

Is it possible to write a template function for which a particular type of

  • 0

Is it possible to write a template function for which a particular type of classes have some functions or overloaded operators? For instance

template <typename T>
void dosomething(const T& x){
    std::cout << x[0] << std::endl;
}

In this context I’m assuming that x is a class that behaves like an array, that is, I have overloaded the [] operator as well as the << so that it can work with std::cout.

The actual code that I have is slightly different but gcc is giving me

error: subscripted value is neither array nor pointer

This must be because it doesn’t know that I’m expecting T to be of some class that overloads the [] operator. Does anyone know if it is possible to overcome this? I’d like to let c++ know that the particular type T will have the [] overloaded.

  • 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-24T23:54:51+00:00Added an answer on May 24, 2026 at 11:54 pm

    You might need to provide a little more detail, as this short example works for me:

    #include <iostream>
    #include <vector>
    
    template <typename T>
    void dosomething(const T& x){
        std::cout << x << std::endl;
    }
    
    template <typename T>
    void dosomething_else(const T& x){
        std::cout << x[0] << std::endl;
    }
    
    int main() {
        dosomething< int >(1) ;
        std::vector< int > vec( 3, 1 );
        //dosomething< std::vector< int > >(vec);
        dosomething_else< std::vector< int > >(vec);
    }
    

    However, if you were to uncomment this line you would get a compiler error as std::vector doesn’t implement the << operator:

    //dosomething< std::vector< int > >(vec);
    

    When you say this your thinking is on the right track:

    I’d like to let c++ know that the particular type T will have the []
    overloaded.

    However, the C++ compiler will actually search for [] operator at compile-time for any functions that request it. If there is no [] operator defined, you will get a compiler error. For example, this will cause a compiler error if inserted into the main() function:

    dosomething_else< int >(1);
    

    You get this error message, similar to what you suggest in the question:

    test.cpp: In function 'void dosomething_else(const T&) [with T = int]':
    test.cpp:19:   instantiated from here
    test.cpp:11: error: subscripted value is neither array nor pointer
    

    You can actually check if the [] exists at compile-time using the method outlined in this question:
    How to check whether operator== exists?

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

Sidebar

Related Questions

I'd like to write a templated function which changes its behavior depending on template
I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type
Is it possible to write a template that changes behavior depending on if a
Possible Duplicate: C++ HTML template framework, templatizing library, HTML generator library Planning to write
Like the title tells: Is it possible to write a PHP-Function in a XSL-Document
Possible Duplicate: What is the difference between "typename" and "class" template parameters? When defining
Possible Duplicate: Is it possible to write a C++ template to check for a
Is it possible to write a C++(0x) metafunction that determines whether a type is
how do I write a function pointer as default template argument, I'am guessing to
I have a simple class: template<size_t N, typename T> class Int { bool valid(size_t

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.