-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add static_assert
s for template parameters of basic_fixed_string
#30
Comments
template <typename T>
struct not_a_vector {
static_assert(std::is_trivially_copyable_v<T>,
"\n\nnot_a_vector<>T -- can handle only trivially "
"copiable types!\n\n");
} ;
// no warning even if -Wall is used
using should_not_compile_but_it_does
= not_a_vector<std::string> ; Consider instead controling the type definition. template <typename T,
std::enable_if_t< std::is_trivially_copyable_v<T>, int > = 0
>
class better_not_a_vector {
} ;
using should_not_compile_and_it_does_not =
better_not_a_vector<std::string>;
using should_compile_and_it_does =
better_not_a_vector<bool>;
|
I don't consider this as an issue. It will be a compile-time error anyway, when the type alias is used. |
if For example: // this is inside dudkinlib.h
#include <not_a_vector> // not owned by dudkin ltd
// no warning even if -Wall is used
namespace dudkin {
using vector_of_strings // should_not_compile_but_it_does
= not_a_vector<std::string> ;
} It might happen this will go unnoticed for some months, and then it will be noticed by some important customer. // DUDKINLIB paying customer code, few months later
#include <dudkinlib.h>
dudkin::vector_of_strings customer_ ; // does not compile What is the solution? |
Another example from
template<typename T, std::size_t k>
class hyper_log_log
{
public:
static_assert(k >= 4 && k <= 30, "k must be in a range [4; 30]");
} ;
using timebomb_type = hyper_log_log<bool, 1> ;
// few months (or years) after:
// timebomb_type boom ; Just a forward declared type constrained template is enough to stop wrong type definitions. // forward declaration of a type constrained template
template <typename T, std::size_t k,
std::enable_if_t<k >= 4 && k <= 30, int> = 0>
class better_hyper_log_log ; User (or you) can not introduce types that will not compile few months or years down the line // does not compile
// using impossible_type = better_hyper_log_log<bool, 1> ;
using ok_type = better_hyper_log_log<bool, 13> ; This peculiarity is what one might describe as "C++ foot gun with a timer" ... |
Actually, this is the correct solution template< typename T, std::size_t k>
#if __cplusplus >= 202002L
// C++20 (and later) code
requires // requires-clause (ad-hoc constraint)
(k >= 4 && k <= 30)
#endif
class constrained_hyper_log_log ;
using constrained_type = constrained_hyper_log_log<bool, 13> ;
|
Example from implementation of
basic_string_view
by Microsoft: https://github.com/microsoft/STL/blob/main/stl/inc/xstring#L1224-L1230The text was updated successfully, but these errors were encountered: