c++ - How to expect a static_assert failure and deal with it using Boost.Test framework? -
c++ - How to expect a static_assert failure and deal with it using Boost.Test framework? -
if have method accepting template parameter should convertible to, base_of, or same type type returned, how should do?
for instance, consider method:
template <class t> class ifoo { public: template <class servicet> t* as() { static_assert(std::is_same< t, servicet >::value || std::is_convertible< t, servicet >::value || std::is_base_of< servicet, t >::value, "ifoo< t >::as< servicet >() requires servicet base of operations of t"); ... } };
now, boost_check it!
class {}; class b {}; boost_auto_test_case(registering_incompatible_types_should_raise_a_static_assert_failure) { boost_check_static_assert_failure(ifoo< a* >().as< b* >()); }
i want boost_check compile fine , pass well. but, want user code fail compiling when :
void myawesomemethod() { auto result = ifoo< a* >().as< b* >(); ... }
any idea?
for information, compile-time failure prevent compilation... why here after all.
i can think of 2 ways proposing:
using sfinae using compiler specific optionssfinae
literally, sfinae means: substitution failure not error. applies template context , allows functions prove inadequate discarded quietly overload set. utilize of sfinae has given rising thought of concept checking , categorization of properties using traits used back upwards checks.
in case, means if can somehow set look want test in context in sfinae may apply seek , observe particular function has been discarded.
for example:
#include <iostream> #include <utility> struct foo {}; struct bar {}; template <typename t> auto foo(t t) -> decltype(std::declval<foo>() + t) { std::cout << "t\n"; } void foo(...) { std::cout << "ellipsis\n"; } int main() { foo(bar()); }
yields:
ellipsis
(see ideone) though there no operator+(foo, bar)
defined anywhere.
unfortunately, may not work in cases (unsure yet), should portable on compliant compilers.
compiler specific
another possibility utilize compiler specific features. compiler test suites must verify compilers correctly diagnose errors, , in case emit error when static_assert
status met. hence compilers have hooks this.
for example, in clang test suite 1 can find semacxx/static-assert.cpp
file:
// run: %clang_cc1 -fsyntax-only -verify %s -std=c++0x int f(); static_assert(f(), "f"); // expected-error {{static_assert look not integral constant expression}} static_assert(true, "true not false"); static_assert(false, "false false"); // expected-error {{static_assert failed "false false"}} void g() { static_assert(false, "false false"); // expected-error {{static_assert failed "false false"}} } class c { static_assert(false, "false false"); // expected-error {{static_assert failed "false false"}} }; template<int n> struct t { static_assert(n == 2, "n not 2!"); // expected-error {{static_assert failed "n not 2!"}} }; t<1> t1; // expected-note {{in instantiation of template class 't<1>' requested here}} t<2> t2; template<typename t> struct s { static_assert(sizeof(t) > sizeof(char), "type not big enough!"); // expected-error {{static_assert failed "type not big enough!"}} }; s<char> s1; // expected-note {{in instantiation of template class 's<char>' requested here}} s<int> s2;
the -fsyntax-only
avoid code generation , -verify
means compiler checks expected-note
, expected-warning
, expected-error
specified correctly met.
if not, compiler homecoming error code. of course, compiler specific.
c++ unit-testing c++11 boost-test
Comments
Post a Comment