Note that there are some explanatory texts on larger screens.

plurals
  1. POMake conversion to a native type explicit in C++
    text
    copied!<p>I'm trying to write a class that implements 64-bit ints for a compiler that doesn't support <code>long long</code>, to be used in existing code. Basically, I should be able to have a typedef somewhere that selects whether I want to use <code>long long</code> or my class, and everything else should compile and work.</p> <p>So, I obviously need conversion constructors from <code>int</code>, <code>long</code>, etc., and the respective conversion operators (casts) to those types. This seems to cause errors with arithmetic operators. With native types, the compiler "knows" that when <code>operator*(int, char)</code> is called, it should promote the <code>char</code> to <code>int</code> and call <code>operator*(int, int)</code> (rather than casting the <code>int</code> to <code>char</code>, for example). In my case it gets confused between the various built-in operators and the ones I created.</p> <p>It seems to me like if I could flag the conversion operators as explicit somehow, that it would solve the issue, but as far as I can tell the <code>explicit</code> keyword is only for constructors (and I can't make constructors for built-in types).</p> <p>So is there any way of marking the casts as explicit? Or am I barking up the wrong tree here and there's another way of solving this? Or maybe I'm just doing something else wrong...</p> <p>EDIT:</p> <p>A little clarification on what I want to do: I have a project that uses 64-bit ('long long`) operations in many places, and I am trying to port it to a platform that <em>doesn't</em> have built-in support for 64-bit variables/operations. Even though I have the source for the project, I would really prefer not to have to go over the thousands of places where built-in operators and C-style casts are and change them around.</p> <p>As for the code itself, the project has the following definitions and kinds of code:</p> <pre><code>typedef long long i64; // I would like to only have to change this to use my class instead of "long long" int a; unsigned b; int c = ((i64)a*b)&gt;&gt;32; </code></pre> <p>As for the class implementation, I have the following:</p> <pre><code>class Long64 { public: Long64(); Long64(const Long64&amp;); Long64(int); Long64(long); Long64(unsigned int); Long64(unsigned long); operator int() const; operator long() const; operator unsigned int() const; operator unsigned long() const; friend Long64 operator*(int l, const Long64&amp; r); friend Long64 operator*(const Long64&amp; l, int r); friend Long64 operator*(long l, const Long64&amp; r); friend Long64 operator*(const Long64&amp; l, long r); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload