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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:13:10+00:00 2026-06-10T16:13:10+00:00

Suppose, I want to develop a generic library which should be usable with number-like

  • 0

Suppose, I want to develop a generic library which should be usable with number-like types including double and user-defined types. The problem, I’m facing right now is that I don’t know how to write the return type of a function template much like this one:

template<class T>
auto transmogrify(T x)
-> ???
{
    using std::abs;
    return abs(x)+2.0;
}

The using declaration makes this function template’s body work for primitive types because these don’t have an associated namespace (and hence, there is no ADL). But I want transmogrify to use specialized abs functions in case the author of a user-defined type provides his own abs function. I can’t simply use

-> decltype( abs(x)+2.0 )

because this would not work for, say, doubles since std::abs is not in scope (as far as I can tell). But writing

-> decltype( std::abs(x)+2.0 )

would disable ADL. But disabling ADL is not an option. Also, the value returned by a specialized abs function might not be of type T but some other type.

Any ideas on how to solve the return type issue while (a) keeping ADL and (b) falling back on some default function (like std::abs in this case) for types that don’t provide a specialized abs.

  • 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-10T16:13:12+00:00Added an answer on June 10, 2026 at 4:13 pm

    Use a separate namespace, where you can put the using clause. This prevents the namespace pollution, since the using clause only applies to that namespace. I would recommend naming it something unique, so you don’t accidentally spread it around.

    namespace transmog_detail
    {
       using std::abs;
    
       template<class T>
       auto transmogrify(T x) -> decltype(abs(x) + 2.0)
       {
          return abs(x) + 2.0;
       }
    }
    
    // Then pull it into the current namespace, as recommended by @LucDanton.
    using transmog_detail::transmogrify;
    
    // Or if there is a reason, you can forward. 
    // template<class T>
    // auto transmogrify(T x)
    // -> decltype(transmog_detail::transmogrify(x))
    // {
    //    return transmog_detail::transmogrify(x);
    // }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to make a Web application which uses a fixed width and
Suppose I want to implement an interpreter for a functional language. I would like
Suppose we want two constructors for a class representing complex numbers: Complex (double re,
Suppose I want to create my own desktop envoriment for Linux, without X. Like
Suppose I develop a social networking site and I decide that I want the
I want to develop a Java EE6 application that contains some Web-GUI functionality. Suppose
suppose if i want to develop a windows chat application then a chat server
The general goal: Suppose for instance I want to develop a very pluggable issue
I want to develop a program which keeps track of the amount of data
Suppose I want to add two buffers and store the result. Both buffers are

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.