- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
For metaprogramming debugging, I frequently need the ability the print a type at compiletime, but some of the standard tricks don't seem to work. For example, boost::mpl::print
It would be nice to have the ability to do it without a compiler failure, but I will take a fail if I need to.
One of the usual tricks is to use an incomplete type. For example, the following kind of works:
etk::print_fail
Note that I cannot put a ";" at the end of print_fail, and a typedef of it doesn't work either or it compiles with no error or compile warning. It seems to me that if an incomplete type is not used, the C++ compiler ignores them and keeps compiling.
Any ideas on how to do this better or print without a failure?
Thanks,
Jesse
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In order to get an error about the incomplete type you need to actually instantiate the template or use the type by creating an object, i.e.
etk::print_fail
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks!
Can you think of any tricks to get it to print a warning and not stop compiling?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you're using icc (i.e. Linux or Mac) then you could use the deprecated attribute, i.e.:
template
int main() {
print_fail
return 0;
}
sptel5-345> icpc -c t.cpp
t.cpp(7): warning #1478: class "print_fail
print_fail
^
sptel5-346>
If you're using icl (Windows) I think you should be able to use the deprecated declspec.
Judy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Judy,
I am on Windows.
I tried the following, but had no luck generating a warning:
[cpp]templatestruct __declspec(deprecated) print_warning {};[/cpp]
[cpp] int main() { print_warningx; return 0; } [/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sorry about that - that would work for a non template class butdoesn'tseem to currently work for a template class (I've submitted a bug report).
How about this?
!% cat t.cpp
template
print_warning
int main() {
__alignof(f()); // Microsoft lets you use it in an unevaluated context with a warning
return 0;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks. That did it. Here is my macro if anyone is interested:
[bash]templatestruct incomplete; //The following needs to be used within a function! #define ETK_PRINT_TYPE(T) {incomplete temp_print_##T(); _alignof(temp_print_##T());} //Example usage: int main(){ ETK_PRINT_TYPE(int) } [/bash]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
this issue has been fixed in compiler 15.0.
Now, icl will emit the warnings for both cases:
struct __declspec(deprecated) print_warning {}; template <class T> struct __declspec(deprecated) print_warning_temp {}; int foo () { print_warning x; print_warning_temp<int> y; return 0; }
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page