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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:29:32+00:00 2026-05-27T04:29:32+00:00

I want to templatize the casting operator with a specialization for bool, but it’s

  • 0

I want to templatize the casting operator with a specialization for bool, but it’s not working.

template<typename T> //Don't know if the fact that C is templated itself is relevant here
class C
{ 
...
        template<typename U> operator U() const { return another_namespace::MyCast<U>(*this); }
        template<> operator bool() const { return IsValid(); } 
};

This gives me (g++ 4.6)

explicit specialization in non-namespace scope ‘class C< T>’

Now just

operator bool() const { return IsValid(); }

by itself works, as does MyCast (it’s a friend function declared in an external namespace). Is there any way I can get the intended behHoavior here?

Edit: I have subsequently found this, looks like the same basic question, however the answer (which gives a very complicated solution) looks designed specifically for strings. Also, the issue there turned out to be ambiguity, which I think is not the problem here–I get a very different compiler error message.

  • 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-05-27T04:29:33+00:00Added an answer on May 27, 2026 at 4:29 am

    You can overload conversion operators, so simply using a non-templated version of the operator bool() should work:

    template<typename T>
    class C
    { 
    ...
            template<typename U> operator U() const { ... }
            operator bool() const { ... } 
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like template <typename T> void foo(const T& t) {
I want to align my member variables based on a class template type but
I am trying to create a custom template, but I want the template to
I want to template one function for code reuse and specialization. The function signature
I want to template the >> and << operators in a class, but I
I am creating a custom control. I want the template for this control to
i want a specialize template in a pointer-to-member-function case. Is there a way to
I want to have a template class that looks something like what I have
I want to create a template class in C#, for example: public class Foo<T>
I want to use this pure HTML/CSS template for my ASP.NET website: http://sub3.tanguay.de I

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.