ritter
ritter

Reputation: 7709

Let the compiler make the final choice which type to use

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?

Upvotes: 3

Views: 110

Answers (3)

Mark B
Mark B

Reputation: 96271

You can't do that. Once you've said a variable is Integer<int> that is the variable's type. What you can do is make the underlying rep of the Integer vary depending on which constructor is used, something like this:

template<class T>
class Integer {
  // cut out
    Integer() : rep_(IntRep()) {}
    explicit Integer(int val) : rep_(IntLitRep(val)) {}

private:
    boost::variant<IntRep, IntLitRep> rep_;
};

Then you can easily determine which variant version is active and utilize different code paths when needed.

EDIT: In this case even though the type of the Integer is the same, you can easily use template functions to make it appear that it's behaving as two separate types (since the rep changes effective type).

Upvotes: 1

joeking
joeking

Reputation: 2096

You can't do that, exactly anyways. But using "auto" you can come close.

// The default case
Integer<int> MakeInt()
{
    return Integer<int>();
}

// The generic case
template <class T>
Integer<T> MakeInt(T n)
{
    return Integer<T>(n);
}

// And specific to "int"
Integer<IntegerLit> MakeInt(int n)
{
    return Integer<IntegerLit>(n);
}

auto a = MakeInt();
auto b = MakeInt(2);
auto c = MakeInt('c');
auto d = MakeInt(2.5);

Upvotes: 3

MSalters
MSalters

Reputation: 179927

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").

Upvotes: 5

Related Questions