g++编译类型转换报错_确定C++类型在编译时是否可以转换为
另⼀种类型
g++编译类型转换报错
When writing generic code, using template meta-programming techniques, it is sometimes uful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generate a log or trace file for debugging purpos. The relationship of the types may have significance.
使⽤模板元编程技术编写通⽤代码时,有时知道⼀个类型是否可转换为另⼀种类型很有⽤。 ⼀个很好的例⼦是,如果您正在编写诊断⼯具来编写代码以⽣成⽤于调试⽬的的⽇志或跟踪⽂件。 类型之间的关系可能具有重要意义。
老老年
This relationship can be determined with a little bit of meta-template trickery along with a special function prototype that takes an ellipsis ... as its formal parameter list.
可以通过⼀点点元模板欺骗以及带有省略号...作为其正式参数列表的特殊功能原型来确定这种关系。
A little background for tho who don't know, the ... ellipsis in C and C++ parlance is a special function parameter that means the function will accept any type and any number of parameters and is often ud in C along with var_args to create functions that take variable arguments, functions such as printf() or scanf(), for example.
百里山水画廊对于⼀些不了解的⼈来说,背景有点... C和C ++措辞中的省略号是⼀个特殊的函数参数,这意味着该函数将接受任何类型和任意数量的参数,并且经常在C中与var_args⼀起使⽤以创建带有可变参数的函数,例如printf()或scanf()之类的函数。
费用会计An important factor to this technique is that, in C++, a function can be overloaded with an ellipsis version and it will be
called if and only if no other function of the same name can be found to match the calling parameter list. We take advantage of this by declaring (but not defining) two overloads of with same function name; one that take a reference to the type we're looking to e if we can convert to and the other takes the ... ellipsis.
这项技术的⼀个重要因素是,在C ++中,⼀个函数可以使⽤省略号版本重载,并且当且仅当找不到相同名称的其他函数与调⽤参数列表匹配时,该函数才会被调⽤。 我们通过声明(但未定义)两个具有
相同函数名的重载来利⽤这⼀点。 ⼀个引⽤我们要查看的类型,以查看是否可以转换为该类型,另⼀个引⽤...省略号。
The trick is to have the ellipsis version return a type that is a different size to that of the more specific function. At compile time the compiler will u static polymorphism to decide which function to call and we can then u the sizeof operator on the function call to get the size of the function's return type that the compiler decided matched the calling parameter. If the types are convertible then the return type size will be that of the specific function taking a reference to the convertible type, otherwi the size will be that of the generic function that has the ellipsis parameter.
诀窍是让省略号版本返回与更具体函数的⼤⼩不同的类型。 在编译时,编译器将使⽤静态多态性来确定要调⽤的函数,然后我们可以在函数调⽤中使⽤sizeof运算符来获取编译器决定与调⽤参数匹配的函数返回类型的⼤⼩。 如果类型是可转换的,则返回类型的⼤⼩将是引⽤可转换类型的特定函数的⼤⼩,否则,⼤⼩将是具有省略号参数的通⽤函数的⼤⼩。
Note, neither of the functions actually needs to be defined -- only declared -- becau neither of them is actually ever called; there is no runtime cost to this technique
注意,这两个函数实际上都不需要定义-只需声明-因为它们实际上都没有被调⽤过。 此技术没有运⾏
时成本
This can all be wrapped up in a simple little template meta-function to simplify usage. Below is a
所有这些都可以包装在⼀个简单的⼩模板元函数中,以简化⽤法。 下⾯是⼀个⼈为的例⼦...
#include <iostream>
// Some types
struct A{};
struct B:A{};
struct C{};
template <typename T1, typename T2>
自制水果罐头struct is_convertible
{
承包协议合同范本
private:
struct True_ { char x[2]; };
如懿传嘉妃结局struct Fal_ { };
static True_ helper(T2 const &);
static Fal_ helper(...);
public:
static bool const YES = (
sizeof(True_) == sizeof(is_convertible::helper(T1())) );
};
template <typename T1, typename T2>
void foo(T1 const & t1, T2 const & t2)
{
if(is_convertible<T1, T2>::YES)
{
std::cout << "Type t1 is convertible to t2" << std::endl; }
el
{
std::cout << "Type t1 is not convertible to t2" << std::endl; }
}
最好吃的泡面>古风散文
int main(void)
{
struct A a;
struct B b;
struct C c;
foo(b,a);
foo(c,a);
}
g++编译类型转换报错