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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:28:20+00:00 2026-06-13T21:28:20+00:00

This question requires knowledge of C++ template meta-programming as (indirectly) expression templates are involved.

  • 0

This question requires knowledge of C++ template meta-programming as (indirectly) expression templates are involved. I say indirectly because its not directly a question on expression templates, but involves C++ type computations. If you don’t know what that is please don’t answer this question.

To avoid putting out a question without enough background information let me elaborate a bit on the general problem I am trying to solve and then go to the more specific parts.

Suppose you have a library that provides Integers that the user can do calculations with just like with ints.
Furthermore it is possible to construct a Integer from an int. Just like:

Integer<int> i(2);

Internally my Integer class is a class template:

template<class T>
class Integer {
  // cut out
};

So I can define it on whatever integer type I like.

Now without changing the API, I would like to change the library in a way that if Integer was constructed from an int it should be represented internally by a different type, say IntegerLit. The reason for this is that I can speed up some calculation knowing that an instance of Integer was created from an int (can pass it as a int argument to a function instead of as a general object described by a base pointer + separate data. This just as a comment.)

It is essential that the type is different when constructing from an int because I need the compiler to take up different code paths depending on whether constructed from an int or not. I cannot do this with a runtime data flag. (The reason in short: The compiler generates a function that takes either an int or the above mentioned more general type of object depending on the type.)

Having this said I run into a problem: When the uses does something like this:

Integer<int> a,b(2);

a = b + b;

Here a should be the general Integer and b the specialized IntegerLit. However, my problem is how to express this in C++ as the user is free to use the very same type Integer to define her variables.

Making the types polymorphic, i.e. deriving IntegerLit from Integer won’t work. It looks fine for a moment. However since the user creates instances of Integer (the base class) that won’t work because it is the base class the compiler sticks into the expression tree (this is why expression templates are involved in the question). So again no distinction is possible between the two cases. Doing a RTTI check a la dynamic cast is really not what I want on that point.

More promising seems to be adding a literal template parameter bool lit to the type saying if it was constructed from an int or not. The point is to not specify the conversion rule for one not literal one but do specify it for the other case.

However, I can’t get that to work. The following code only compiles (GCC 4.7 C++11) if not constructing from an int. Otherwise it fails because the Integer is not specified with true as the value for lit. So the compiler searches the default implementation which doesn’t have the conversion rule. It is not an option changing the API and requiring to write Integer<int,true> when constructing from an int.

template<class T,bool lit=false>
class Integer
{
public:
  Integer() {
    std::cout << __PRETTY_FUNCTION__ << "\n";
  }
  T F;
};

template<>
template<class T>
class Integer<T,true>
{
public:
  Integer(int i) {
    std::cout << __PRETTY_FUNCTION__ << "\n";
  }
  T F;
};

I am starting wondering if something like this is possible with C++.

Is there maybe a new feature of C++11 that can help here?

  • 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-13T21:28:21+00:00Added an answer on June 13, 2026 at 9:28 pm

    No, this isn’t how C++ works. If you define b as Integer b, then it’s an Integer. THis applies regardless of the expression which is subsequently used to initialize b.

    Also, consider the following: extern Integer b;. There’s an expression somewhere else that initializes b, yet the compiler still has to figure out here what type b has. (Not “will have”, but “has”).

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

Sidebar

Related Questions

Sorry for this long post. The question is however small but requires full detail.
This question is about good programming practices and avoiding potential holes. I read Joshua
This is a question for experienced C/C++ developpers. I have zero knowledge of compiling
(This question is a toughie... it might require knowledge of NTFS and/or the use
Edit: This question was asked due to inadequate knowledge of the EventMachine . With
Sorry if this is an obvious question, but my knowledge of asp.net is a
I'm asking this question with little/no knowledge of SMS gateways and it's relevant technologies,
This question is probably going to illustrate a lack of knowledge on my part
This question is directly related to this SO question I posed about 15 minutes
This question is kind of a follow up to this question I asked a

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.