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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:54:26+00:00 2026-05-10T17:54:26+00:00

Assume I have a function template like this: template<class T> inline void doStuff(T* arr)

  • 0

Assume I have a function template like this:

template<class T> inline void doStuff(T* arr) {   // stuff that needs to use sizeof(T) } 

Then in another .h filee I have a template class Foo that has:

public: operator T*() const; 

Now, I realize that those are different Ts. But If I have a variable Foo<Bar> f on the stack, the only way to coerce it to any kind of pointer would be to invoke operator T*(). Yet, if call doStuff(f), GCC complains that doStuff can’t take Foo<Bar> instead of automatically using operator T*() to coerce to Bar* and then specializing the function template with Bar as T.

Is there anything I can do to make this work with two templates? Or does either the argument of the template function have to be a real pointer type or the template class with the coercion operator be passed to a non-template function?

  • 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. 2026-05-10T17:54:27+00:00Added an answer on May 10, 2026 at 5:54 pm

    GCC is correct. In template arguments only exact matches are considered, type conversions are not. This is because otherwise an infinite (or at least exponential) amount of conversions could have to be considered.

    If Foo<T> is the only other template that you’re going to run in to, the best solution would be to add:

    template<typename T> inline void doStuff(const Foo<T>& arr) {     doStuff(static_cast<T*>(arr)); } 

    If you are having this issue with a lot of templates, this one should fix it:

    #include <boost/type_traits/is_convertible.hpp> #include <boost/utility/enable_if.hpp> template<template <typename> class T, typename U> inline typename boost::enable_if<typename boost::is_convertible<T<U>, U*>::type>::type doStuff(const T<U>& arr) {     doStuff(static_cast<U*>(arr)); } 

    It’s a bit verbose though 😉

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First of all, the condition should read cardCount == 2… May 11, 2026 at 5:20 pm
  • Editorial Team
    Editorial Team added an answer Check out: Wikipedia - IP Multicast Specifically the addressing section. May 11, 2026 at 5:20 pm
  • Editorial Team
    Editorial Team added an answer I don't see that this makes sense. Unless your service… May 11, 2026 at 5:20 pm

Related Questions

I'm using C++ templates to pass in Strategy functors to change my function's behavior.
Weird problem in Django with forms : I have a Form.class defined like this
In templates, where and why do I have to put typename and template on
Just wondering: When I add restrict to a pointer, I tell the compiler that

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.