Community
cancel
Showing results for 
Search instead for 
Did you mean: 
jlperla
Beginner
114 Views

How do you display a typename at compiletime with 11.1?

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 doesn't seem to work with this compiler.

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:

template struct incomplete;
template struct print_fail : incomplete {};

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

0 Kudos
7 Replies
Judith_W_Intel
Employee
114 Views

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();

jlperla
Beginner
114 Views

Thanks!

Can you think of any tricks to get it to print a warning and not stop compiling?

Judith_W_Intel
Employee
114 Views

If you're using icc (i.e. Linux or Mac) then you could use the deprecated attribute, i.e.:

template struct __attribute ((deprecated)) print_fail {};

int main() {
print_fail p;
return 0;
}

sptel5-345> icpc -c t.cpp
t.cpp(7): warning #1478: class "print_fail" (declared at line 4) was declared "deprecated"
print_fail p;
^

sptel5-346>

If you're using icl (Windows) I think you should be able to use the deprecated declspec.

Judy

jlperla
Beginner
114 Views

Thanks Judy,

I am on Windows.

I tried the following, but had no luck generating a warning:

[cpp]template  struct __declspec(deprecated) print_warning {};[/cpp]
[cpp]
int main()
{
  print_warning x;
  return 0;
}
[/cpp]

Judith_W_Intel
Employee
114 Views

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 struct print_warning;

print_warning f(); // incomplete return type

int main() {

__alignof(f()); // Microsoft lets you use it in an unevaluated context with a warning

return 0;

}

jlperla
Beginner
114 Views

Thanks. That did it. Here is my macro if anyone is interested:

[bash]template  struct 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]

JenniferJ
Moderator
114 Views

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;
}

 

Reply