1

The program below compiles in VS 2010, but not in a recent version of Mingw. Mingw gives me the error "conversion from int to non-scalar type 'tempClass(it)' requested". Class "it" is just a simple class to use in the template for the purpose of illustration.

#include <iostream>
#include <string>

using namespace std;

template <class T>
class tempClass{
    public:
    T theVar;

    tempClass(){}

    tempClass(T a){
        theVar = a;
    }

/*  tempClass <T> & operator = (T a){
            (*this) = tempClass(a);
            return *this;
    }
*/
};

class it{
    public:

    int n;

    it(){}

    it(int a){
        n = a;
    }
};

int main(){
    tempClass <it> thisOne = 5;         // in MinGW gives error "conversion from int to non-scalar type 'tempClass(it)' requested"
    cout << thisOne.theVar.n << endl;   // in VS 2010 outputs 5 as expected
}

Commenting in/out the assignment operator portion doesn't seem to make a difference - I didn't expect it to, I just included it because I also hope to do things like tempClass <it> a = 5; a = 6;, in case this is relevant to the answer.

My question is, how can I get this syntax to work as desired?

  • 5
    Unrelated, but why are you using tools that are a decade old? A lot has happened in the past 10 years, both with the C++ standard and with your tools standards compliance. – Jesper Juhl Sep 22 '20 at 21:00
  • 1
    MinGW is largely obsolete considering the [Windows Subsystem for Linux](https://learn.microsoft.com/en-us/windows/wsl/about) provides a much, much better experience. It has modern compilers as well. A 10 year old C++ compiler is effectively junk. – tadman Sep 22 '20 at 21:03
  • I just had a computer with a C++ compiler already installed from a while back. I only installed Mingw because I wanted to use variadic templates. Either way, how can I get this syntax to work? – Humid Morning Sep 22 '20 at 21:06
  • @tadman the raison d'etre for MinGW and derivatives is to use gcc to build code that uses the Windows API , so it is not obsoleted by WSL – M.M Sep 22 '20 at 21:38
  • 2
    @M.M If you prefer that to Visual Studio then knock yourself out I guess. I see a lot of people struggling with MinGW and they're just trying to compile, period, no particular target in mind. – tadman Sep 22 '20 at 21:39
  • @tadman someone who can't install a compiler is probably not the target audience for this discussion – M.M Sep 22 '20 at 21:42

1 Answers1

2

MinGW is correct to refuse the code since it relies on two implicit user-defined conversions. One from int to it and one from it to tempClass<it>. Only one user-defined implicit conversion is allowed.

The below works since it only requires one implicit conversion:

tempClass<it> thisOne = it(5);

You can also let the constructor do the conversion which will let you do
tempClass<it> thisOne = 5;. In the below example the constructor will accept any argument and will try to initialize theVar with it. If U is convertible to T, it'll compile and work as expected. Otherwise you'll get a compilation error about an invalid conversion.

template<class T>
class tempClass {
public:
    template<typename U>
    tempClass(U a) : theVar(a) {}

//private:
    T theVar;
};

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108