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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:08:30+00:00 2026-05-17T23:08:30+00:00

I have a question about templates. I would like to have a templated class

  • 0

I have a question about templates. I would like to have a templated class that contains, say, an array of either float’s or double’s.

I can write a clone() function that duplicates it. No problem. However, I would like to have another function called cast() that does a translation back and forth between double and float. This has been somewhat discussed before but I don’t think the problem is the same:

stackoverflow.com/questions/714213/c-template-casting

The problem I have is a compiler error, not a linker error. The error message is:

main.cpp: In function `void caster()':
main.cpp:63: error: expected primary-expression before "double"
main.cpp:63: error: expected `;' before "double"
main.cpp:64: error: expected primary-expression before "float"
main.cpp:64: error: expected `;' before "float"
main.cpp:65: error: expected primary-expression before '>' token
main.cpp:65: error: expected primary-expression before ')' token

I dumped the code below. The lines 63, 64, and 65 are where I commented with “Error here”.

By the way, my compiler is “GNU C++ version 3.4.5 20051201 (Red Hat 3.4.5-2) (x86_64-redhat-linux) compiled by GNU C version 3.4.5 20051201 (Red Hat 3.4.5-2)”.

After some Googling, it turns out that someone has already experienced this problem:

gcc.gnu.org/ml/gcc-help/2006-04/msg00022.html

There is a solution here:

gcc.gnu.org/ml/gcc-help/2006-04/msg00023.html

But when the original poster asks as to why it works, the answer is not very clear:

gcc.gnu.org/ml/gcc-help/2006-04/msg00025.html

Unfortunately, the link is dead, and I don’t own the third edition of Stroustrup. Now, I have my fix, my code works. But, Stackoverflow, why does it work?


#include <stdio.h>

// =================== This would be the header ===================
template <class T>
class foo
{
public:
             foo(const T val) {d_data = new double; *d_data = val;}
    virtual ~foo() {delete d_data;};

    foo* clone() const;

    template<class U>
    foo<U>* cast() const;

private:
    double *d_data;
};

// =================== This would be the implementation of the class ===================
template<class T>
foo<T>* foo<T>::clone() const
{
    return new foo<T>(*d_data);
}

template<class T>
template<class U>
foo<U>* foo<T>::cast() const
{
    return new foo<U>(*d_data);
}

template class foo<float>;
template class foo<double>;

template foo<float>* foo<float>::cast() const;
template foo<float>* foo<double>::cast() const;
template foo<double>* foo<float>::cast() const;
template foo<double>* foo<double>::cast() const;

// =================== Using the class ===================
template <class T>
void caster()
{
    foo<double> *f1 = NULL;
    foo<float>  *f2 = NULL;
    foo<T>      *f3 = NULL;

    // I am looking at something that compiles
    // I don't care about linking for now
    // This will crash at runtime because of
    // NULL, but that's just an example

    f1->cast<double>(); // compiler OK
    f1->cast<float>();  // compiler OK
    f1->cast<T>();      // compiler OK

    f2->cast<double>(); // compiler OK
    f2->cast<float>();  // compiler OK
    f2->cast<T>();      // compiler OK

    f3->cast<double>(); // Error here
    f3->cast<float>();  // Error here
    f3->cast<T>();      // Error here

    f3->foo<T>::template cast<double>(); // It works!
    f3->foo<T>::template cast<float>();  // It works!
    f3->foo<T>::template cast<T>();      // It works!
}

int main(int argc, char **argv)
{
    return 0;
}
  • 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-17T23:08:31+00:00Added an answer on May 17, 2026 at 11:08 pm
    f3->cast<double>(); // Error here 
    

    In this line, the compiler does not know if the < after f3->cast is supposed to mean the beginning of template arguments or if it is a less than comparison operator.

    To explicitly specify that it stands for beginning of template argument. The correct way is

    f3->template cast<double>();
    

    In summary, The .template notation (and similar notations such as ->template) should be used only inside templates and only if they follow something that depends on a template parameter (such as the expression f3 which depends upon the template parameter T)

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

Sidebar

Related Questions

I have question about parsing in Html helper : I have sth like: @foreach
I have a question about template classes. For example, take this class template<class TBase>
I have the below question, and would like to know if I have interpreted
This is a little contrived, but say I have a class interface like this:
I may be going about this backwards... I have a class which is like
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all
I have question about normalization. Suppose I have an applications dealing with songs. First
I have question about interpreting strings as packed binary data in C++. In python,
i have question about YAJLiOS parser... I have next json data : {{ body

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.