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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:54:36+00:00 2026-05-16T02:54:36+00:00

I have defined a class in C++ which holds an array of scalars of

  • 0

I have defined a class in C++ which holds an array of scalars of type T for which I want to define operators like sin, cos, etc. For defining the meaning of sin applied on an object of this class I need to know the meaning of sin applied on the single scalar type T. This means I need to use appropriate math libraries (corresponding to the scalar type T) within the class. Here’s the code as it is now:

template<class T>
class MyType<T>
{
    private:
        std::vector<T> list;

    // ...

        template<class U> friend const UTP<U> sin(const UTP<U>& a);
        template<class U> friend const UTP<U> cos(const UTP<U>& a);
        template<class U> friend const UTP<U> tan(const UTP<U>& a);

    //...
};

template<class T> const UTP<T> sin(const UTP<T>& a)
{
   // use the sin(..) appropriate for type T here 
   // if T were double I want to use double std::sin(double)
   // if T were BigNum I want to use BigNum somelib::bigtype::sin(BigNum)
}

Currently, I have code that exposes the appropriate math library (using namespace std;) and then use ::sin(a) inside the sin function for my class MyType. While this works, it seems like a major hack.

I see that C++ traits can be used to store instance specific information (like which set of math functions to use when T is double, when T is BigNum, etc..)

I want to do something like this: (I know this doesn’t compile but I hope this conveys what I want to do)

template<T>
struct MyType_traits {
};

template<>
struct MyType_traits<double> {
    namespace math = std;
};

template<>
struct MyType_traits<BigNum> {
    namespace math = somelib::bigtype;
};

and then in redefine my MyType class as:

template<T, traits = MyType_traits<T> >
class MyType
{
// ...
}

and then use traits::math::sin in my friend function. Is there a way in which I can obtain the correct namespace (parameterized by T) containing the math functions?

  • 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-16T02:54:36+00:00Added an answer on May 16, 2026 at 2:54 am

    Isn’t argument-dependent look-up good enough?

    #include <cmath>
    #include <iostream>
    
    namespace xxx {
    class X
    {
    };
    
    X sin(X) { return X(); }
    } //xxx
    
    std::ostream& operator<< (std::ostream& os, xxx::X)
    {
        return os << "X";
    }
    
    template <class T>
    void use_sin(T t)
    {
        using std::sin; //primitive types are not in a namespace,
                        //and with some implementation sin(double) etc might not be available
                        //in global namespace
        std::cout << sin(t) << '\n';
    }
    
    int main()
    {
        use_sin(1.0);
        use_sin(xxx::X());
    }
    

    This would work for X, because sin(X) is defined in the same namespace as X. If you expect that not to be so, this probably won’t help…

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

Sidebar

Related Questions

In my application I have a class which has properties of user-defined types like
I have a Model which has some constants defined, like below: class Order(models.Model): WAITING
I have a class called MainGame, which is defined like this in my .h:
I define a base class which have a Long primary key , just like
I have one base class which holds a map for function pointers like this
I have defined a ConfigurationProperty class, which is basicly a key-value pair (with key
I have three tables which are defined as: class User(Base): __tablename__ = 'users' id
i have a class User which extends Ebean Model. And i defined a dbfile
I have a class called Path for which there are defined about 10 methods,
I have a CCounter class which holds and integer value protected by mutex. I've

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.