Connecting Tech Pros Worldwide Forums | Help | Site Map

Should this short (but abstruse) program compile?

Howard Gardner
Guest
 
Posts: n/a
#1: Jul 14 '06
/*

As it sits, this program won't compile for me. It will compile
using either of the versions of tpt that I've commented out.
Which versions SHOULD compile?

I think that the code is interesting because I think that it is
on the path to building a useful non-intrusive type introspection
facility. The version that uses "has" indicates the direction.

*/
#include <ostream>
#include <cstddef>

template< typename x >
x * faux_ptr();

//template< typename x, size_t = sizeof( int * ) >
// struct tpt{};

//template< typename x, size_t = sizeof( faux_ptr< int >() ) >
// struct tpt{};

//struct has{ int memvar; };
//template< typename x, size_t = sizeof( faux_ptr< has >()->memvar ) >
// struct tpt{};

template< typename x, size_t = sizeof( faux_ptr< x >() ) >
struct tpt{};

template< typename x >
bool fnc( tpt< x );

tpt< int inst;

size_t size = sizeof( fnc( inst ) );

int main()
{
using namespace std;
cout << size << endl;
}

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 14 '06

re: Should this short (but abstruse) program compile?


Howard Gardner wrote:
Quote:
/*
>
As it sits, this program won't compile for me. It will compile
using either of the versions of tpt that I've commented out.
Which versions SHOULD compile?
>
I think that the code is interesting because I think that it is
on the path to building a useful non-intrusive type introspection
facility. The version that uses "has" indicates the direction.
>
*/
#include <ostream>
#include <cstddef>
>
template< typename x >
x * faux_ptr();
>
//template< typename x, size_t = sizeof( int * ) >
// struct tpt{};
>
//template< typename x, size_t = sizeof( faux_ptr< int >() ) >
// struct tpt{};
>
//struct has{ int memvar; };
//template< typename x, size_t = sizeof( faux_ptr< has >()->memvar ) >
// struct tpt{};
>
template< typename x, size_t = sizeof( faux_ptr< x >() ) >
struct tpt{};
Why don't you simply make it

template< typename x, size_t = sizeof( x* ) >
struct tpt{};

Isn't it the same?

Incidentally, the original code compiles fine for me with VC++ 2005
and doesn't compile with online Comeau trial.
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 );

? Comeau compiles the code in this case.
Quote:
>
tpt< int inst;
>
size_t size = sizeof( fnc( inst ) );
>
int main()
{
using namespace std;
cout << size << endl;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Howard Gardner
Guest
 
Posts: n/a
#3: Jul 15 '06

re: Should this short (but abstruse) program compile?


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

Howard Gardner
Guest
 
Posts: n/a
#4: Jul 15 '06

re: Should this short (but abstruse) program compile?


Sent this 10 hours ago, not showing up on server. Sorry if it's a dupe.

Victor Bazarov wrote:
Quote:
Howard Gardner wrote:
Quote:
>/*
>>
>As it sits, this program won't compile for me. It will compile
>using either of the versions of tpt that I've commented out.
>Which versions SHOULD compile?
>>
>I think that the code is interesting because I think that it is
>on the path to building a useful non-intrusive type introspection
>facility. The version that uses "has" indicates the direction.
>>
>*/
>#include <ostream>
>#include <cstddef>
>>
>template< typename x >
> x * faux_ptr();
>>
>//template< typename x, size_t = sizeof( int * ) >
>// struct tpt{};
>>
>//template< typename x, size_t = sizeof( faux_ptr< int >() ) >
>// struct tpt{};
>>
>//struct has{ int memvar; };
>//template< typename x, size_t = sizeof( faux_ptr< has >()->memvar ) >
>// struct tpt{};
>>
>template< typename x, size_t = sizeof( faux_ptr< x >() ) >
> struct tpt{};
>
Why don't you simply make it
>
template< typename x, size_t = sizeof( x* ) >
struct tpt{};
>
Isn't it the same?
>
Incidentally, the original code compiles fine for me with VC++ 2005
and doesn't compile with online Comeau trial.
>
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 );
>
? Comeau compiles the code in this case.
>
Quote:
>tpt< int inst;
>>
>size_t size = sizeof( fnc( inst ) );
>>
>int main()
>{
> using namespace std;
> cout << size << endl;
>}
>
V
Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 16 '06

re: Should this short (but abstruse) program compile?


Howard Gardner wrote:
Quote:
[...]
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.
Sure.
Quote:
>
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.
It makes sense to contact them directly if you have a question about
some behavoiur of their compiler. They are very knowledgeable in C++
and can describe to you what parts of the Standard apply in such cases.
Quote:
[...]
>
On my compiler, sizeof( no ) is 1, and sizeof( yes ) is 2. It prints:
>
1
1
So does it on VC++ 2005.
Quote:
>
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
I have no explanation to share, sorry. But it's an interesting case.
Once you get it resolved, post your results please, for everybody's
benefit. Thanks!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Howard Gardner
Guest
 
Posts: n/a
#6: Jul 16 '06

re: Should this short (but abstruse) program compile?


Victor Bazarov wrote:
Quote:
Once you get it resolved, post your results please, for everybody's
benefit. Thanks!
I started a thread over in comp.std.c++.
Howard Gardner
Guest
 
Posts: n/a
#7: Jul 21 '06

re: Should this short (but abstruse) program compile?


Victor Bazarov wrote:
Quote:
Howard Gardner wrote:
>
I have no explanation to share, sorry. But it's an interesting case.
Once you get it resolved, post your results please, for everybody's
benefit. Thanks!
>
V
This got resolved. Comeau looked at the code that I originally posted.
It is an open issue. Here's the link they sent me.

http://www.comeaucomputing.com/iso/cwg_active.html#339
Closed Thread