 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
E.D. Guest
|
Posted: Thu Dec 30, 2004 8:16 pm Post subject: Why doesn't this compile? |
|
|
Hi!
In the following code I get a compile error when I call f1(). I use
g++ 3.2.2. Is this a compiler problem and if not where does this code
violate the standard?
Thanx in advance,
ED.
#include <set>
template <typename T>
struct F {
typedef std::set<F> Set;
};
class C {
public:
template<typename T>
void f1(const typename F<T>::Set& s) {
}
template<typename T>
void f2(const std::set<T>& s) {
}
};
int main(int argc, char** argv) {
std::set<int> s;
C c;
c.f1(s); // compile error
c.f1<int>(s); // also does not compile
c.f2(s); // works
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ralph Zhang Guest
|
Posted: Fri Dec 31, 2004 1:14 pm Post subject: Re: Why doesn't this compile? |
|
|
You probably mean
template <typename T>
struct F {
typedef std::set<T> Set;
// ^
};
And then
c.f1<int>(s);
compiles.
As for
c.f1(s);
because the compiler cannot deduce the template parameter, it won't
compile at all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
L.Suresh Guest
|
Posted: Fri Dec 31, 2004 1:18 pm Post subject: Re: Why doesn't this compile? |
|
|
Deduction fails because its a non deduced context. Qualified type names
is a non deduced context.
14.8.2.4 Deducing template arguments from a type
"
If a template parameter is used only in nondeduced contexts and is not
explicitly specified, template argument deduction fails.
The nondeduced contexts are:
- The nested-name-specifier of a type that was specified using a
qualified-id.
- A type that is a template-id in which one or more of the
template-arguments is an expression that references a
template-parameter.
"
--lsu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Dec 31, 2004 1:22 pm Post subject: Re: Why doesn't this compile? |
|
|
E.D. wrote:
| Quote: | In the following code I get a compile error when I call f1(). I use
g++ 3.2.2. Is this a compiler problem and if not where does this code
violate the standard?
Thanx in advance,
ED.
#include <set
template
struct F {
typedef std::set
|
Did you mean to write
typedef std::set<T> Set;
? 'F' is not a type-id here, it's a template-id, so who knows what
the compiler is trying to do. And 'std::set' needs a type-id as its
first argument.
| Quote: | };
class C {
public:
template<typename T
void f1(const typename F
}
template<typename T
void f2(const std::set
}
|
It seems that if you wanted 'f1' and 'f2' to have the same argument types,
then you definitely wanted F<T>::Set to be std::set<T>, not std::set<F>
| Quote: | };
int main(int argc, char** argv) {
std::set<int> s;
C c;
c.f1(s); // compile error
|
Will still fail to compile because it can't figure out 'T', since 'Set'
can be defined differently in specific instantiations of 'F'. 'Set' is
a dependent name, and there are special rules that allow the compiler
not to try to figure out T in those cases, IIRC.
| Quote: | c.f1<int>(s); // also does not compile
|
Will compile when you make the change as I suggested.
| Quote: | c.f2(s); // works
return 0;
}
|
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|