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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:40:49+00:00 2026-06-10T08:40:49+00:00

There is a code like this. const std::string DeviceTypeStrings[] ={ A, B, C, D,

  • 0

There is a code like this.

const std::string DeviceTypeStrings[] ={ "A", "B", "C", "D", "E" };

enum DeviceTypes { A = 0, B, C, D, E };

template <DeviceTypes T> class DeviceType;
template <DeviceTypes T> std::ostream& operator<< (std::ostream& output, const DeviceType<T>& dev);

template <DeviceTypes T> class DeviceType {
    public:
         static const int value = T;
         static const std::string string;
         friend std::ostream & operator<< <>(std::ostream & output, const DeviceType<T> & deviceType );
};
template <DeviceTypes T> const std::string DeviceType<T>::string = DeviceTypeStrings[T];
template <DeviceTypes T> std::ostream & operator<< (std::ostream & output, const DeviceType<T> & deviceType ){
    if ( DeviceType<T>::string.find(' ') != std::string::npos ){
        return output << "\"" << DeviceType<T>::string << "\""; 
    } else {
        return output << DeviceType<T>::string;
    }   
}

int main () {
    DeviceType<A> myType;
    std::cout << myType << std::endl;
    return 0;    
}

Note there is a “<>” after the operator<< inside class DeviceType, what does “<>” mean? If you could, why does it has to be there?

  • 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-10T08:40:50+00:00Added an answer on June 10, 2026 at 8:40 am

    It simply means that the friend declaration refers to a particular specialization of function template operator << (declared previously), not to some yet undeclared ordinary non-template function operator <<.

    Which specialization this friend declaration refers to is determined by the argument deduction mechanism, i.e. the actual template arguments are implicitly derived from the parameter types used in friend declaration. For this reason there’s no need to specify template arguments in <> explicitly, but an empty pair of <> is still required.

    In other words, the author of the code could’ve stated explicitly

    friend std::ostream & operator<< <T>(std::ostream & output, 
                                         const DeviceType<T> & deviceType );
    

    (note the explicit T in <T>). However, since the compiler can figure it out by itself (derive it from the type of the second argument), it is perefectly possible to put just an empty pair of <> there.

    Now, if the code just said

    friend std::ostream & operator<<(std::ostream & output, 
                                     const DeviceType<T> & deviceType );
    

    (i.e. no <> at all), it would befriend an ordinary (non-template) function operator <<, which is not what the author wanted.

    Overload resolution feature that works in this friend declaration can be illustrated without any friend declarations by the following simple example

    void foo(int);
    template <typename T> void foo(T);
    
    int main() {
      foo(42);      // calls non-template function
      foo<int>(42); // calls template function, explicit template argument given
      foo<>(42);    // calls template function, template argument deduced by compiler
    }
    

    When you want to tell the compiler that you specifically want to refer to a template version of the function, you have to include triangular brackets into the reference, even if there’s nothing between them.

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

Sidebar

Related Questions

My colleague's code looked like this: void copy(std::string const& s, char *d) { for(int
This code: void Controller::write(const std::string& str) { std::cout << Writing: [ << str <<
There's a really obvious refactoring opportunity in this (working) code. bool Translations::compatibleNICodes(const Rule& rule,
I have some methods like this in my class: static std::string numberToString(int n); static
I use Boost.Serialization to serialize a std::map. The code looks like this void Dictionary::serialize(std::string
I just found my code like this does not compile right? Is there any
I have some code that looks like this. There is also an autoincrement field
I have code which needs to do something like this There is a list
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
Possible Duplicate: Convert std::string to const char* or char* is there any to get

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.