在这段代码中:
// CompileTimeWarnings.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <type_traits>
using namespace std;
#define __STR1__(x) #x
#define __LOC__ __FILE__ "("__STR1__(__LINE__)") : warning : "
// collisions.cpp
template<class T, class T1>
struct mismatch
{
//enum {value = is_signed<T>::value && is_signed<T1>::value};
static const bool value; //= is_signed<T>::value && is_signed<T1>::value;
};
template<class T, class T1>
bool mismatch<T,T1>::value = is_signed<T>::value && is_signed<T1>::value;
template<class T>
struct Int
{
};
template<class T, class T1>
int operator+(Int<T> t, Int<T1> t1) …Run Code Online (Sandbox Code Playgroud) 为什么这在GCC 4.5.1中不起作用
#include <iostream>
#include <type_traits>
#include <limits.h>//for INT_MIN
#include <typeinfo>
using namespace std;
#ifdef _MSC_VER
#define UNIVERSAL_INT_MAX LLONG_MAX
#define UNIVERSAL_INT_MIN LLONG_MIN
#endif
#ifdef __GNUC__
#define UNIVERSAL_INT_MAX LONG_LONG_MAX
#define UNIVERSAL_INT_MIN LONG_LONG_MIN
#endif
using namespace std;
int main()
{
cout << UNIVERSAL_INT_MAX << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
"main.cpp|24|error: 'LONG_LONG_MAX' was not declared in this scope"
Run Code Online (Sandbox Code Playgroud)
编辑
#ifdef __GNUC__
#define UNIVERSAL_INT_MAX ( ( 1ULL << numeric_limits< long long >::digits ) - 1 )
#define UNIVERSAL_INT_MAX_plus_three (UNIVERSAL_INT_MAX + 3)
#endif
using namespace …Run Code Online (Sandbox Code Playgroud) 我希望能够做到这样的事情:
void f(int*p = nullptr)
{
if (!p)
{
//HERE I WOULD LIKE TO HAVE AN MSG THAT WOULD BE DISPLAYED DURING COMPILATION AS A WARNING POSSIBLY
}
}
Run Code Online (Sandbox Code Playgroud) template<class T, class U>
struct is_convertible
{
typedef char yes;
typedef struct
{char _[2];}no;
static yes test(U);
static no test(...);
enum {value = (sizeof(test(0)) == sizeof(yes)) ? 1 : 0};
//THE PART I'M INTERESTED IN IS (test(0)). Why 0 (zero) works here?
};
Run Code Online (Sandbox Code Playgroud)
请在代码中查看评论.
为什么类定义中的 this (static_assert) 不起作用?
template<class IntT, IntT low = IntT(), IntT high = IntT()>
struct X
{
static_assert(std::is_same<decltype(low),decltype(high)>::value,"Different types not allowed");
};
int _tmain(int argc, _TCHAR* argv[])
{
int low, high;
X<char,1,'a'> x;//HERE I SHOULD GET ERROR
cout << sizeof(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)