[英]How to ouput diagnostic messages during compiling time to tell which overloaded function was called
For example, I have overloaded functions, f()
, they are defined as 例如,我有重载函数
f()
,它们被定义为
struct Test {
int i = 1;
Test(double d) : i (d){}
};
struct MyClass {
int f(double d) {
cout << "parent\n";
return d + 3 ; }
};
struct Child : public MyClass {
using MyClass::f; // if we comment out this line, the output will be different
double f(Test t) {
cout << "child\n";
return t.i * 9;
}
};
int main() {
Child c;
std:: cout << c.f(1.0) << std::endl;
}
The function f
in Child class overloaded the function in parent. 子类中的函数
f
重载了父类中的函数。 How can we generate diagnostic information during compiling time, such that we know which function is chosen by the compiler for cf(1.0)
? 我们如何在编译期间生成诊断信息,以使我们知道编译器为
cf(1.0)
选择了哪个函数?
Note that if I remove the line using MyClass:f;
请注意,如果我
using MyClass:f;
删除该行using MyClass:f;
, the output will be different but there are no error for compiling. ,输出将有所不同,但编译没有错误。
Update: 更新:
Based on @PW 's answer, there are two ways to modify the existing class to use template instantiation to output the static assertion, 根据@PW的答案,有两种方法可以修改现有类以使用模板实例化来输出静态断言,
template <typename T>
struct ConditionalFalse {
static constexpr bool value = false;
};
struct Test {
int i = 1;
Test(double d) : i (d){}
};
struct MyClass {
template<typename T>
int f(double i) {
static_assert(ConditionalFalse<T>::value, "Parent");
return i + 3 ; }
};
struct Child : public MyClass {
using MyClass::f;
template<typename T>
double f(Test t) {
static_assert(ConditionalFalse<T>::value, "Child");
return t.i * 9;
}
};
template<typename T>
struct MyClassT {
int f(double i) {
static_assert(ConditionalFalse<T>::value, "ParentT");
return i + 3 ; }
};
template<typename T>
struct ChildT : public MyClassT<T> {
using MyClassT<T>::f;
double f(Test t) {
static_assert(ConditionalFalse<T>::value, "ChildT");
return t.i * 9;
}
};
int main() {
Child c;
std:: cout << c.f<int>(1.0) << std::endl;
ChildT<int> ct;
std:: cout << ct.f(1.0) << std::endl;
}
I am still wondering if this is possible without
modifying the class or the member function to be a template? 我仍然想知道是否可以在
without
将类或成员函数修改为模板的情况下实现?
A simple static_assert(false_condition, "...")
would fail at compile time always. 一个简单的
static_assert(false_condition, "...")
总是在编译时失败。 So you need something that happens at compile-time and is still dependent on something else. 因此,您需要在编译时发生的事情,并且仍然依赖于其他事情。 You can do this if your
MyClass
and Child
were templates. 如果
MyClass
和Child
是模板,则可以执行此操作。
You will also need a dependent expression, and so you can use a helper struct to get something that will be type-dependent. 您还将需要一个从属表达式,因此您可以使用帮助程序结构来获取与类型有关的内容。
Now your code will look like this: 现在,您的代码将如下所示:
#include<iostream>
template <typename T>
struct ConditionalFalse { static constexpr bool value = false; };
struct Test {
int i = 1;
Test(double d) : i (d){}
};
template <typename T>
struct MyClass {
int f(double d) {
static_assert(ConditionalFalse<T>::value, "The Parent function is called\n");
return d + 3 ; }
};
template <typename T>
struct Child : public MyClass<T> {
using MyClass<T>::f; // if we comment out this line, the output will be different
double f(Test t) {
static_assert(ConditionalFalse<T>::value, "The Child function is called\n");
return t.i * 9;
}
};
int main() {
Child<int> c; //The template parameter doesn't matter
std::cout << c.f(1.0) << std::endl;
}
Depending on whether using MyClass<T>::f;
取决于是否
using MyClass<T>::f;
is commented or not you will get different assertions. 被评论或未评论,您将得到不同的断言。
If it is not commented, you will get: 如果未评论,您将获得:
error: static assertion failed: The Parent function is called
static_assert(ConditionalFalse<T>::value, "The Parent function is called\n");
^~~~~~~~~~~~~~~~~~~
If it is commented, you will get: 如果有评论,您将获得:
<source>:22:19: error: static assertion failed: The Child function is called
static_assert(ConditionalFalse<T>::value, "The Child function is called\n");
^~~~~~~~~~~~~~~~~~~
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.