Kris
Kris

Reputation: 539

Is there anyway to avoid warning/error template instantiation backtrace?

Spent some time on it and have absolutely no idea if it's possible. Therefore thought I will ask here then. So, is there any clever way of forcing not to print template backtrace when showing warning/error on gcc/clang?

Example:

template<int I = 0, typename = void>
struct warn {
    unsigned : I;
};
struct hello_world {};

template<int I>
class a : warn<I, hello_world> {};

template<int I>
class b : a<I>{};

template<int I>
class c : b<I> {};

template<int I>
class d : c<I> {};

int main() {
    d<80>{};
}

gives:

test.cpp:3:5: warning: size of anonymous bit-field (80 bits) exceeds size of its type; value will be truncated to 32 bits
    unsigned : I;
    ^
test.cpp:8:11: note: in instantiation of template class 'warn<80, hello_world>' requested here
class a : warn<I, hello_world> {};
          ^
test.cpp:11:11: note: in instantiation of template class 'a<80>' requested here
class b : a<I>{};
          ^
test.cpp:14:11: note: in instantiation of template class 'b<80>' requested here
class c : b<I> {};
          ^
test.cpp:17:11: note: in instantiation of template class 'c<80>' requested here
class d : c<I> {};
          ^
test.cpp:20:2: note: in instantiation of template class 'd<80>' requested here
        d<80>{};
        ^
1 warning generated.

So, the expected result would be for example:

test.cpp:3:5: warning: size of anonymous bit-field (80 bits) exceeds size of its type; value will be truncated to 32 bits
    unsigned : I;
    ^
test.cpp:8:11: note: in instantiation of template class 'warn<80, hello_world>' requested here
class a : warn<I, hello_world> {};

There is -ftemplate-backtrace-limit=1 -ferror-limit=1, but I was wondering if there is a possibility to do it from the source code.

Why I need such functionality? Well, I'm using concepts simulations via enable-if, but unfortunately I have template conversion operator within my concept construction and can't just return the value and static assert or enable-if it, because the information is not available anymore. Therefore I thought that maybe warning + concept would do the thing. Let's say I will still have the concept and print one line warning with useful stuff and after that disable the function as usual with enable-if.

Upvotes: 1

Views: 1698

Answers (1)

Tom Knapen
Tom Knapen

Reputation: 2277

You can get close using aliases instead of classes/structs:

template<int I = 0, typename = void>
struct warn {
    unsigned : I;
};
struct hello_world {};

template<int I>
using a = warn<I, hello_world>;

template<int I>
using b = a<I>;

template<int I>
using c = b<I>;

template<int I>
using d = c<I>;

int main() {
  (void)d<80>{};
}

Output:

test.cpp:3:5: warning: size of anonymous bit-field (80 bits) exceeds size of its type; value will be truncated to 32 bits
    unsigned : I;
    ^
test.cpp:20:9: note: in instantiation of template class 'warn<80, hello_world>' requested here
  (void)d<80>{};
        ^
1 warning generated.

Upvotes: 2

Related Questions