Home Suppress compiler warning for specific template code
Reply: 0

Suppress compiler warning for specific template code

user1319
1#
user1319 Published in April 24, 2018, 8:34 am

I have implemented a template class to detect the convertability of two type (following the method described in the book Modern C++ Design by Andrei Alexandrescu Section 2.7).

The implementation I made is as follows:

#include <utility>
#include <iostream>

template<typename T, typename U>
class Conversion
{
private:
    using Small = char;
    using Big = class{ char dummy[2]; };
    static Small Test(U);
    static Big Test(...);
public:
    enum
    {
        exists = (sizeof(Test(std::declval<T>())) == sizeof(Small)) // Warning related to conversion.
    };
};

int main()
{
    std::cout << "Conversion int to float :" << Conversion<int, float>::exists << "\n";
    return 0;
}

On compiling this code on Visual Studio 2013(Visual C++ 2013), I get following warning related to conversion from int to float

warning C4244: 'argument' : conversion from 'int' to 'float', possible loss of data.

As it was an implicit requirement here, is there a way to suppress this warning?

I just want to suppress it for this case alone. If such conversion is being done in other places, compiler should still generate the warning.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30831 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO