禁止针对特定模板代码的编译器警告

saj*_*jas 5 c++ templates type-conversion compiler-warnings visual-studio-2013

我已经实现了一个模板类来检测两种类型的可转换性(遵循Andrei Alexandrescu第2.7节"Modern C++ Design"一书中描述的方法).

我的实施如下:

#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;
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2013(Visual C++ 2013)上编译此代码时,我收到有关从int到float的转换的以下警告

警告C4244:'argument':从'int'转换为'float',可能会丢失数据.

由于这是一个隐含的要求,有没有办法抑制这个警告?

我只是想单独为这个案子压制它.如果在其他地方进行此类转换,编译器仍应生成警告.

use*_*670 1

首先,如果有人有抑制/纠正/警告的冲动,那么他很可能做错了什么。在这种情况下,可以重写测试器来执行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;
}
Run Code Online (Sandbox Code Playgroud)

在线编译器在vc++上/W4没有错误