c***@gmail.com
2008-08-05 05:11:21 UTC
Hi all,
I've got a problem which appears to be related to the ambiguity
between
template <> brackets and greater and less than symbols... Here's a
snippet of
code which I'm having trouble with:
--------------------------------------------------
struct Tag
{
typedef int type;
};
struct A
{
template<typename TagT>
typename TagT::type find(typename TagT::type i)
{
return i;
}
};
template<typename T>
int foo()
{
int i = 0;
T t;
// The following doesn't compile but is well-formed if t.find is a
template
// member of T, and Tag is a type.
i = t.find<Tag>(1);
// While the below should be functionally equivalent
//i = static_cast<A>(t).find<Tag>(42);
return i;
}
int main()
{
int i = foo<A>();
return 0;
}
--------------------------------------------------
This is just a cut-down toy version, but it shows the main thing I'm
having
problems with. What I want is to be able to use an expression like
t.find<Tag>(1);
where t is a template type, Tag is some tag struct, and find is a
template
member function. When I try to compile the above example, g++ gives
an error
like
main.cpp: In function 'int foo()':
main.cpp:22: error: expected primary-expression before '>' token
The error goes away if I write an explicit cast:
static_cast<A>(t).find<Tag>(1);
I can see how the expression could be ambiguous, since the following
compiles
fine:
--------------------------------------------------
struct B
{
static const int find = 1;
};
int bar()
{
B b;
int Tag = 1;
int j = b.find < Tag > (1);
return j;
}
--------------------------------------------------
- in this case Tag and b.find are variables, but the exact same syntax
is
used.
Am I doing something wrong here? Is this somehow forbidden by the C++
spec?
Otherwise I guess it's a compiler error...
Any help appreciated, thanks for your time.
~Chris F.
I've got a problem which appears to be related to the ambiguity
between
template <> brackets and greater and less than symbols... Here's a
snippet of
code which I'm having trouble with:
--------------------------------------------------
struct Tag
{
typedef int type;
};
struct A
{
template<typename TagT>
typename TagT::type find(typename TagT::type i)
{
return i;
}
};
template<typename T>
int foo()
{
int i = 0;
T t;
// The following doesn't compile but is well-formed if t.find is a
template
// member of T, and Tag is a type.
i = t.find<Tag>(1);
// While the below should be functionally equivalent
//i = static_cast<A>(t).find<Tag>(42);
return i;
}
int main()
{
int i = foo<A>();
return 0;
}
--------------------------------------------------
This is just a cut-down toy version, but it shows the main thing I'm
having
problems with. What I want is to be able to use an expression like
t.find<Tag>(1);
where t is a template type, Tag is some tag struct, and find is a
template
member function. When I try to compile the above example, g++ gives
an error
like
main.cpp: In function 'int foo()':
main.cpp:22: error: expected primary-expression before '>' token
The error goes away if I write an explicit cast:
static_cast<A>(t).find<Tag>(1);
I can see how the expression could be ambiguous, since the following
compiles
fine:
--------------------------------------------------
struct B
{
static const int find = 1;
};
int bar()
{
B b;
int Tag = 1;
int j = b.find < Tag > (1);
return j;
}
--------------------------------------------------
- in this case Tag and b.find are variables, but the exact same syntax
is
used.
Am I doing something wrong here? Is this somehow forbidden by the C++
spec?
Otherwise I guess it's a compiler error...
Any help appreciated, thanks for your time.
~Chris F.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]