Skip to content

Commit 774fb43

Browse files
committed
Disabled 'is_palindrome' for 'const char*'
1 parent 4fcb7af commit 774fb43

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

doc/is_palindrome.qbk

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ All of the variants of `is_palindrome` take their parameters by value or const r
7979

8080
* If you use version of 'is_palindrome' without custom predicate, 'is_palindrome' uses default 'operator==' for elements. If you want use custom predicate, you must redefine 'operator=='.
8181

82+
* Don't use 'const char*' with 'is_palindrome', because 'const char*' is always non-palindromic ('\0' at the end). If you will try to compile 'is_palindrome' with 'const char*', you will get an error.
83+
8284
[endsect]
8385

8486
[/ File is_palindrome.qbk

include/boost/algorithm/is_palindrome.hpp

+16
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ bool is_palindrome(const R& range, Predicate p)
102102
return is_palindrome(boost::begin(range), boost::end(range), p);
103103
}
104104

105+
//Disable is_palindrome for const char* because it work not properly.
106+
//Please use string_view for const char* cases.
107+
//Here we use dirty hack to disable 'is_palindrome' with 'const char*'
108+
//URL: http://stackoverflow.com/questions/14637356/static-assert-fails-compilation-even-though-template-function-is-called-nowhere
109+
template<typename T>
110+
struct foobar : std::false_type
111+
{ };
112+
113+
template<typename T = int>
114+
bool is_palindrome(const char* str)
115+
{
116+
static_assert(foobar<T>::value, "Using 'is_palindrome' for 'const char*' is dangerous, because result is always false"
117+
"(reason: '\0' in the end of the string). You can use string_view in this case");
118+
return false;
119+
}
120+
105121
}}
106122

107123
#endif // BOOST_ALGORITHM_IS_PALINDROME_HPP

0 commit comments

Comments
 (0)