Home Suppress compiler warning for specific template code
Reply: 3

Suppress compiler warning for specific template code

sajas
1#
sajas Published in 2018-01-11 08:37:40Z

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.

VTT
2#
VTT Reply to 2018-01-11 11:40:46Z

The first thing is that if someone has an urge to suppress a /correct/ warning then he is most likely doing something wrong. In this case tester can be rewritten to perform explicit conversion. This will also allow it to handle user-defined explicit conversion operators (that are not covered by your original code or ::std::is_convertible):

#include <utility>
#include <type_traits>
#include <iostream>

template<typename Anything> class
Void
{
    public: using type = void;
};

template<typename T, typename U, typename Enabled = void> class
Conversion
: public ::std::false_type {};

template<typename T, typename U> class
Conversion<T, U, typename Void<decltype(static_cast<U>(::std::declval<T>()))>::type>
: public ::std::true_type {};

struct foo{ explicit operator int(void) const; };

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

online compiler, no error with /W4 on vc++

Michael Foukarakis
3#
Michael Foukarakis Reply to 2018-01-11 08:41:25Z

Use #pragma warning(suppress, …, as per the documentation.

Jens
4#
Jens Reply to 2018-01-11 09:15:09Z

There is also std::is_convertible in C++11:

#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";
    std::cout << "Conversion int to float :" << Conversion<float, int>::exists << "\n";

    std::cout << "Conversion int to float :" << std::is_convertible<int,float>::value << "\n";
    std::cout << "Conversion int to float :" << std::is_convertible<float, int>::value << "\n";
    return 0;
}

I don't have a local Visual C++ here, but the online compiler does not generate a warning for std::is_convertible with warning level 4:

Warning(s):
source_file.cpp(15): warning C4244: 'argument': conversion from 'std::ios_base::iostate' to 'float', possible loss of data
source_file.cpp(21): note: see reference to class template instantiation 'Conversion<int,float>' being compiled
source_file.cpp(15): warning C4244: 'argument': conversion from 'float' to 'int', possible loss of data
source_file.cpp(22): note: see reference to class template instantiation 'Conversion<float,int>' being compiled
/LIBPATH:C:\boost_1_60_0\stage\lib 
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
Conversion int to float :1
Conversion int to float :1
Conversion int to float :1
Conversion int to float :1
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO