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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:22:12+00:00 2026-05-15T23:22:12+00:00

For some complicated reason, I want to convert any supported type T (coming from

  • 0

For some complicated reason, I want to convert any supported type T (coming from a template) to a list of types I have chosen. For this, I tried using a template structure named “Convert”. For example:

Convert<short>::type should be int
Convert<int>::type should be int
Convert<char>::type should be int
Convert<float>::type should be double
Convert<double>::type should be double
Convert<const char*>::type should be std::string
Convert<std::string>::type should be std::string
etc.

Those above are easy to implement using template specialisation. But there is one case that is causing problems :

Convert<T>::type where T is a functor should be T

To handle this, I think I have to use SFINAE but I can’t manage to make it compile.

The code below gives me “partial specialization cannot match argument list for primary template” (ie. writing “Convert” is forbidden) :

template<typename T, typename = decltype(&T::operator())>
struct Convert<T>       { typedef T type; };

And this one gives me “template parameter not used or deducible in partial specialization” (ie. it thinks that T is not used) :

template<typename T>
struct Convert<typename std::enable_if<std::is_function<typename T::operator()>::value,T>::type>
 { typedef T type; };

I have no idea of what to do, all my attempts result in one of the two errors above.

EDIT: I would like to catch other generic things using the same model, so I can’t just write “typedef T type” in the non-specialized structure.

Thanks

  • 1 1 Answer
  • 1 View
  • 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-15T23:22:13+00:00Added an answer on May 15, 2026 at 11:22 pm

    I’d suggest you use the first approach, but to make it work, you’ll have to

    Declare the master template with one unused template-argument:

    template <class T, class = void> Convert;
    

    Add a void parameter to all specializations of the template you use now.

    Define your “functor specialization” like this:

    template<typename T, typename std::enable_if<std::is_function<typename T::operator()>::value,void>::type>
    

    That means you make the second argument void if it is a functor (so it matches the default template argument) or nonexisting if it isn’t.

    BTW why do you use typename in typename T::operator()? AFAIK, operator() is not a type.

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

Sidebar

Related Questions

I have a function which isn't so complicated, but for some reason, each time
I have some complicated, problem to be solved. Now I need to write such
I have a complicated nested form (Ryan Bates' version) with .live() attached to some
I have a (rather complicated) SQL statement where I select data from lots of
For a clever and complicated reason that I don't really want to explain (because
I have some unicode text that I want to clean up using regular expressions.
Is there any reason to do anything more complicated than one of these two
In several places I have to retrieve some value from a dict, but need
I have some really complicated legacy code I've been working on that crashes when
I found a site with some complicated C puzzles. Right now I'm dealing with

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.