Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to call user defined conversion operator in templated function with MSVC

Tags:

c++

visual-c++

I need to call a templated conversion operator inside a class like this :


struct S {
    template<typename T>
    operator T() const {
        return T{};
    }

    template<typename T>
    T test()
    {
        return operator T();
    }
};

int main(){
    S s;
    s.test<int>();

    return 0;
}

This code compiles with clang but not with MSVC(19), gives me a c2352 error. Is the correct syntax?

Demo

like image 971
fZab Avatar asked Dec 05 '25 18:12

fZab


1 Answers

Syntax is correct, as work around you might be explicit with this->operator T():

template<typename T>
T test()
{
    return this->operator T();
}

Demo

like image 157
Jarod42 Avatar answered Dec 08 '25 07:12

Jarod42



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!