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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:38:43+00:00 2026-06-02T05:38:43+00:00

sample code given below is not compiled in g++. but it’s working on visual

  • 0

sample code given below is not compiled in g++. but it’s working on visual studio.
is it possible to use Template member function inside template class in g++

class Impl
{
public:
        template<class I>
        void Foo(I* i)
        {

        }
};

template<class C>
class D
{
public:
        C c;
        void Bar()
        {
                int t = 0;
                c.Foo<int>(&t);
        }
};

int main()
{
        D<Impl> d;
        d.Bar();
        return 0;
}
  • 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-02T05:38:45+00:00Added an answer on June 2, 2026 at 5:38 am

    Because the statement in question depends on a template parameter, the compiler is not allowed to introspect C until instantiation. You must tell it that you mean a function template:

    c.template Foo<int>(&t);
    

    If you don’t put template there, the statement is ambiguous. For understanding, imagine the following C:

    class C { const int Foo = 5; }; 
    ...
    c.Foo<int>(&t);
    

    It looks to the compiler as if you compare a const int to an int, and comparing the result of that to some adress of &t: (c.Foo<int) > &t.

    The real solution however is to omit the explicit template argument in the function call, and just do:

    c.Foo(&t);
    

    This is correct even in the case where such a C has a non-template member function Foo(int). Generally, write template code with as few assumptions as possible (but not less).

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

Sidebar

Related Questions

The sample code below shows what I am trying to do: template<int NX, int
Simple stuff but somehow not able to crack it, below piece of code gives
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Given the sample code: class Base { public: bool pub; protected: bool prot; };
Given the following code sample: uint8_t i, in, ni; i = in = 2;
Please, consider the code below: template<typename T> bool function1(T some_var) { return true; }
Background Below is a typical piece of Perl code ( sample.pl for the sake
I'm trying to apply the technique described in http://www.drdobbs.com/tools/227500449 With the sample code below,
In this sample code StartActivity is giving error, if i use this in another
Can any one give me a sample code that gets the selected value from

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.