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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:06:54+00:00 2026-06-15T19:06:54+00:00

Given a simple template <typename T> struct X { T x, y; }; ,

  • 0

Given a simple template <typename T> struct X { T x, y; };, I want to provide conversion constructors such that user can write:

X<double> a;
X<int16_t> b = a; // uses implicit conversion ctr (compiles with warning)
X<int16_t> c(a);  // uses explicit conversion ctr (compiles w/o warning)
X<int32_t> d = c; // uses implicit conversion ctr (compiles w/o warning)

I believe that to achieve this goal, I need to implement both, an implicit and an explicit conversion constructor from type U. But it’s not possible to overload on “implicit” and explicit:

template <typename T> struct X {
     X(T x = T(), T y = T()) : x(x), y(y) {}

     // implicit conversion
     template <typename U> 
     X(const X<U>& other) : x(other.x), y(other.y) {}

     // not possible!
     template <typename U> 
     explicit X(const X<U>& other)
         : x(static_cast<T>(other.x))
         , y(static_cast<T>(other.y)) 
     {}

     T x, y;
};

How could I achieve this goal (I guess I can’t…)?

My initial thought was that I need to enable/disable the one or other depending on is_lossless_convertible. So is there a suitable type trait?

I would like to test if a scalar type U is convertible to type T without loss of precision:

using namespace std;
static_assert(is_lossless_convertible<int16_t, int32_t>::value == true);
static_assert(is_lossless_convertible<int32_t, int16_t>::value == false);
static_assert(is_lossless_convertible<int16_t, uint32_t>::value == false);
static_assert(is_lossless_convertible<int32_t, double>::value == true);
static_assert(is_lossless_convertible<double, int32_t>::value == false);

In short, it should yield true if std::is_convertible<U, T>::value == true and if U x; T y = x; would not issue a compiler warning regarding loss of information.

  • 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-15T19:06:55+00:00Added an answer on June 15, 2026 at 7:06 pm

    You can’t overload on explicitness, but you can provide an explicit converting constructor and implicit conversion operator:

    #include <iostream>
    template<typename T> struct S {
       S() {}
       template<typename U> explicit S(const S<U> &) { std::cout << "explicit\n"; }
       template<typename U> operator S<U>() { return S<U>(this); }
    private:
       template<typename U> friend struct S;
       template<typename U> S(const S<U> *) { std::cout << "implicit\n"; }
    };
    
    int main() {
       S<double> sd;
       S<int> si1(sd);
       S<int> si2 = sd;
    }
    

    Output:

    explicit
    implicit
    

    Tested with gcc and clang.

    The explicit converting constructor is preferred to the implicit conversion operator because in the latter case there is an additional (possibly elided) call to the copy constructor of S<int>.

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

Sidebar

Related Questions

Given a simple namespaced route map.namespace :api do |api| api.resources :genres end how can
Given a simple parent/child class structure. I want to use linqkit to apply a
Simple task: given that an article has many comments, be able to display in
I'm writing a simple maths library with a template vector type: template<typename T, size_t
In C++, I have a certain template function that, on a given condition, calls
Given: template<typename T> class A { B b; std::vector<T> vec1; std::vector<T> vec2; } I'd
Given that I have a URL to a page and I can see the
Does VC++ not support default template parameters arguments? This simple code: template <typename T=int>
i Have been given this simple task , I want to connect to facebook
Hi Could anyone give me a sample program to Create an ApplyRemoveConst template that

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.