I'll just get right down to it: Why doesn't line 38 implicitly convert to char (&)[32]?
template <size_t StringSize>
struct StringT
{
private:
char mChars[StringSize];
public:
// Note: CharArray can decay to char*.
typedef char (&CharArray)[StringSize];
CharArray array() { return mChars; }
operator CharArray() { return mChars; }
operator const CharArray() const { return mChars; }
};
#include <iostream>
template<size_t Size>
void f(char (&array)[Size])
{
std::cout << "I am char array with size " << Size << "\n";
}
int main()
{
StringT<32> someText;
// Conversion through method compiles.
f(someText.array());
// Explicit conversion compiles.
f((StringT<32>::CharArray)someText);
// Implicit conversion fails:
// source_file.cpp(38): error C2672: 'f': no matching overloaded function found
// source_file.cpp(38): error C2784: 'void f(char (&)[Size])': could not deduce template argument for 'char (&)[Size]' from 'StringT<32>'
// source_file.cpp(19): note: see declaration of 'f'
f(someText);
}
This is currently just a small experiment, but the implicit conversion is highly neccessary if StringT<>
is to serve the intended purpose - replacing most of the char arrays in a codebase I am working in.
Thanks in advance.