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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:39:10+00:00 2026-05-14T03:39:10+00:00

I have a structure with a template parameter, Stream . Within that structure, there

  • 0

I have a structure with a template parameter, Stream. Within that structure, there is a function with its own template parameter, Type.

If I try to force a specific instance of the function to be generated and called, it works fine, if I am in a context where the exact type of the structure is known. If not, I get a compile error. This feels like a situation where I’m missing a typename, but there are no nested types. I suspect I’m missing something fundamental, but I’ve been staring at this code for so long all I see are redheads, and frankly writing code that uses templates has never been my forte.

The following is the simplest example I could come up with that illustrates the issue.

#include <iostream>

template<typename Stream>
struct Printer {
  Stream& str;
  Printer(Stream& str_) : str(str_) { }

  template<typename Type>
  Stream& Exec(const Type& t) {
    return str << t << std::endl;
  }
};

template<typename Stream, typename Type>
void Test1(Stream& str, const Type& t) {
  Printer<Stream> out = Printer<Stream>(str);
  /****** vvv This is the line the compiler doesn't like vvv ******/
  out.Exec<bool>(t); 
  /****** ^^^ That is the line the compiler doesn't like ^^^ ******/
}

template<typename Type>
void Test2(const Type& t) {
  Printer<std::ostream> out = Printer<std::ostream>(std::cout);
  out.Exec<bool>(t);
}

template<typename Stream, typename Type>
void Test3(Stream& str, const Type& t) {
    Printer<Stream> out = Printer<Stream>(str);
    out.Exec(t);
}

int main() {
  Test2(5);
  Test3(std::cout, 5);
  return 0;
}

As it is written, gcc-4.4 gives the following:

test.cpp: In function ‘void Test1(Stream&, const Type&)’:
test.cpp:22: error: expected primary-expression before ‘bool’
test.cpp:22: error: expected ‘;’ before ‘bool’

Test2 and Test3 both compile cleanly, and if I comment out Test1 the program executes, and I get “1 5” as I expect. So it looks like there’s nothing wrong with the idea of what I want to do, but I’ve botched something in the implementation.

If anybody could shed some light on what I’m overlooking, it would be greatly appreciated.

  • 1 1 Answer
  • 2 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-14T03:39:10+00:00Added an answer on May 14, 2026 at 3:39 am

    You need to tell the compiler that the dependent name Printer<Stream>::Exec is a template:

    out.template Exec<bool>(t);
    

    It’s the same principle as with typename, just that in this case the problematic name is not a type, but a template.

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

Sidebar

Ask A Question

Stats

  • Questions 435k
  • Answers 435k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I've found out how to do it... using firebug Iv'e… May 15, 2026 at 3:38 pm
  • Editorial Team
    Editorial Team added an answer From the error message, you are doing some work inside… May 15, 2026 at 3:38 pm
  • Editorial Team
    Editorial Team added an answer This only affects the case of the option names, the… May 15, 2026 at 3:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.