This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Why this code can't be compiled


Hi!

I'm trying to compile my program and get strange compilation error.
I've read templates chapter of C++ standard but haven't found any
explanation there. I've made a short test that reproduces the problem
and tested it on g++-3.3, g++-4.1, g++-4.3 and icc version 9. All the
compilers gave me an error with rather meanless diagnostic. The code
follows.

//-------------------------- Cut here ----------------------
template<typename T>
class ptr_wrapper
{
public:
	ptr_wrapper(T* val)
		: val(val) {}

	template<typename T1> ptr_wrapper<T1> convert() const
		{ return ptr_wrapper<T1>( (T1*) val); }
private:
	T* val;
};

template<const int N>
struct int_array {
	int a[N];
};

template<const int N>
class B {
public:
	void f();
};

template<const int N>
void B<N>::f()
{
	ptr_wrapper<int_array<N> > a(0);
	ptr_wrapper<char> b(0);
	a.convert<char>();
}

int main()
{
	B<5> b;

	b.f();

	return 0;
}
//-------------------------- Cut here ----------------------

P.S.: Instatiation of ptr_wrapper from e.g. int_array<5> will lead to
successful compilation.

Thanks

--
  Max


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]