Disable compiler warning for specific template code

I implemented a template class for determining the convertibility of two types (following the method described in the book Modern C ++ Design by Andrei Alexandrescu, section 2.7).

The implementation I made is this:

#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;
}

When compiling this code in Visual Studio 2013 ( Visual C ++ 2013 ), I get the following warning related to converting from int to float

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

How was this an implied requirement here, is there a way to suppress this warning?

. , .

+4
3

-, - //, , , - . explicit. explicit ( ::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;
}

-, /W 4 v++

+1

#pragma warning(suppress, …, .

+1

++ 11 std::is_convertible:

#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 do not have local Visual C ++ here but the online compiler does not generate a warning for std::is_convertiblewith 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
+1
source

Source: https://habr.com/ru/post/1692029/


All Articles