KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I like to give helpful errors / messages, and I also want to do so for my static_assert s. The problem is, that they depend on template parameters. Normally, those parameters will get displayed on way or an other due to the error raised, but they are either obscure or not grouped so they make sense. Example: template<class T> struct fake_dependency{ static bool const value = false; }; template<class T, class Tag> struct Foo{ Foo(){} template<class OtherTag> Foo(Foo<T, OtherTag> const&){ static_assert(fake_dependency<T>::value, "Cannot create Foo<T,Tag> from Foo<T,OtherTag>."); } }; int main(){ Foo<int, struct TagA> fA; Foo<int, struct TagB> fB(fA); } Output on MSVC: src\main.cpp(74): error C2338: Cannot create Foo<T,Tag> from Foo<T,OtherTag>. src\main.cpp(84) : see reference to function template instantiation 'Foo<T,Tag>::Foo<main::TagA>(const Foo<T,main::TagA> &)' being compiled with [ T=int, Tag=main::TagB ] One tag is mentioned in the function template itself, the other below with the class template. Not so nice. Lets see what GCC outputs : prog.cpp: In constructor 'Foo<T, Tag>::Foo(const Foo<T, OtherTag>&) [with OtherTag = main()::TagA, T = int, Tag = main()::TagB]': prog.cpp:18:32: instantiated from here prog.cpp:12:5: error: static assertion failed: "Cannot create Foo<T,Tag> from Foo<T,OtherTag>." Much better, but still not really where the static_assert is. And now imagine some more parameters, or more templates, or both. shivers One way to work around that is to use an intermediate struct, which takes both Tags as template parameters: <code
Tags (comma-separated)
Save Edits
Cancel