I have defined a template class buffer
and another class buffer_view
with a template constructor to take any definition of class buffer
. The idea is for buffer_view
to act as a single type reference-ish to any buffer
type.
template<size_t N> class buffer
{
private:
friend class buffer_view;
};
class buffer_view
{
public:
template<size_t N>
buffer_view(const buffer<N>& bfr)
:m_size(N) {}
size_t size() const { return m_size; }
private:
size_t m_size;
};
It seems to work as expected:
void foo(const buffer_view& bview)
{
std::cout << bview.size() << " _ " << std::flush;
}
int main() {
buffer<8> bfr;
buffer_view bview(bfr);
foo(bview);
foo(buffer_view(bfr));
foo(buffer_view(buffer<32>()));
}
8 _ 8 _ 32 _
However, this doesn't work:
buffer_view bview2(buffer<16>());
foo(bview2);
bview2
gets type buffer_view (buffer<16U> (*)())
or buffer_view (* )(buffer<16u> (*)())
?. See error messages:
MSVC 15.1
Error (active) E0415 no suitable constructor exists to convert from "buffer_view (buffer<16U> (*)())" to "buffer_view"
gcc 5.3.0
error: invalid initialization of reference of type 'const buffer_view&' from expression of type 'buffer_view (* )(buffer<16u> (*)())'
note: in passing argument 1 of 'bool foo(const buffer_view&)'
Best I can tell the type buffer_view (* )(buffer<16u> (*)())
is a function pointer to a function that returns buffer_view
and takes as argument a function pointer to a function that takes no arguments and returns buffer<16u>
?
My question then is, what's going on? Why does it not work as expected when used in a named variable but it does when used in a temporary foo(buffer_view(buffer<32>()))
? - the syntax seems similar to me.