One more try :(
Victor Bazarov wrote:
Quote:
>
Why don't you simply make it
>
template< typename x, size_t = sizeof( x* ) >
struct tpt{};
>
Isn't it the same?
Yes.
I need to know why the code that I posted fails, though, because I think
that the code I'm actually interested in fails for the same reason.
What I really want to write is:
template< typename x, size_t = sizeof( faux_ptr< x >()->memvar ) >
struct tpt{};
The reason I want to write that gets even more abstruse (see
my recent posts in c.l.c.moderated if you're interested).
Quote:
>
Incidentally, the original code compiles fine for me with VC++ 2005
Interesting. There's another version below for you to try if you're willing.
Quote:
and doesn't compile with online Comeau trial.
I'm using Comeau. Every time that I've thought it was misbehaving I've
turned out to be wrong. I fully expect to learn that's the case again.
Quote:
>
Quote:
>template< typename x >
> bool fnc( tpt< x );
>
It might be a mistake to try to rely on the default template argument
here. The syntax 'tpt< x >' here might not give the compiler good enough
a hint so it knows to use the default arg. Have you tried
>
>
template< typename x, size_t s >
bool fnc( tpt< x,s );
The entire point of the exercise is to make the compiler evaluate the
sizeof in the default template argument.
Here's a version that's more closely related to my motivation.
#include <ostream>
#include <cstddef>
struct has{ int memvar; };
struct hasnt{};
template< typename x >
x * faux_ptr();
template< typename x, size_t = sizeof( faux_ptr< x >()->memvar ) >
struct tpt{};
typedef char no;
struct yes{ no val[2]; };
template< typename x >
no fnc( ... );
template< typename x >
yes fnc( tpt< x * );
int main()
{
using namespace std;
cout << sizeof( fnc< has >( 0 ) ) << endl;
cout << sizeof( fnc< hasnt >( 0 ) ) << endl;
}
On my compiler, sizeof( no ) is 1, and sizeof( yes ) is 2. It prints:
1
1
Which means that it selects the "no" version of fnc both times.
If tpt is well formed, it should select the "yes" version of fnc and print:
2
1