Is it possible to tell a const char array that does not ends with '\0'
apart from a C-style string in compile time?
Say I want to write two constructors for a class.
One constructor constructs from a C-style string, which ends with '\0'
.
The other constructor, with different behavior, constructs from a const char array that does not ends with '\0'
.
As a minimal example, consider
#include <cstddef>
#include <iostream>
struct Foo
{
Foo(const char *)
{
std::cout << "a C-style string" << std::endl;
}
// Question: How to modify Foo so that a const char array that does not
// ends with '\0' will go to a different constructor?
template<size_t N>
Foo(const char (&)[N])
{
std::cout << "a const char array "
"that does not ends with '\\0'" << std::endl;
}
};
int main()
{
Foo("a C-style string"); // print "a C-style string"
const char a[3] {'a', 'b', 'c'};
Foo foo(a); // print "a C-style string" - can it change?
return 0;
}
Compiled with g++ -std=c++17
.
Question: Is there any way to achieve this?
Maybe I could apply SFINAE techniques but I haven't figured out how to do so.
Note: There are currently several similar but not identical questions on StackOverflow. I didn't find one that directly addresses my question.