H
H9XLrv5oXVNvHiUI
Hi, I have a question about injecting friend functions within template
classes. My question is specific to gcc (version 3.4.5) used in
combination with mingw because this code (or at least code that gets
the same result) works as expected in visualc++. I know that this is
probably not the right behavior for a compiler but it's the kind of
behavior I'm searching for so I was hoping there was a way to do the
same thing in gcc.
As you know when you define a class (with no template) and within the
class you define a friend function, the function is injected in the
scope directly outside of the class itself.
--- code ---
void Function();
int main(int argc, char* argv[])
{
Function(); // This works
}
class Test
{
public:
friend void Function() { printf("Function()"); getchar(); }
};
-- end code --
Now I would like to mimic the same behavior with template classes, but
this code doesn't work:
-- code --
void Function();
int main(int argc, char* argv[])
{
Function(); // This does not work
}
template <typename T>
class Test
{
public:
friend void Function() { printf("Function()"); getchar(); }
};
template class Test<int>;
-- end code --
Specifically I receive an error at link time about Function() not
being defined, which I guess is due to the compiler not considering
the definition of Function to be a non-template function and instead
assuming it to be a template function just because it's defined inside
of a template class.
Now, the funny thing is that if you move the main() BELOW the explicit
instantiation of the template class Test, this code actually compile
and works, that's why I consider it to be a "bug" because
theoretically if the definition of Function() is a template function
(thus not a normal function) it should never be used for the call
inside of the main() function, whether the main() function is below
the instantiation or not. Am I right?
Anyway I'm searching for a way to make the definition of Function()
available for the main() function even if the main() function is above
the template instantiation. Is there a way to accomplish this for gcc?
Like some command-line flag or something? I'd really appreciate that.
Thanks.
classes. My question is specific to gcc (version 3.4.5) used in
combination with mingw because this code (or at least code that gets
the same result) works as expected in visualc++. I know that this is
probably not the right behavior for a compiler but it's the kind of
behavior I'm searching for so I was hoping there was a way to do the
same thing in gcc.
As you know when you define a class (with no template) and within the
class you define a friend function, the function is injected in the
scope directly outside of the class itself.
--- code ---
void Function();
int main(int argc, char* argv[])
{
Function(); // This works
}
class Test
{
public:
friend void Function() { printf("Function()"); getchar(); }
};
-- end code --
Now I would like to mimic the same behavior with template classes, but
this code doesn't work:
-- code --
void Function();
int main(int argc, char* argv[])
{
Function(); // This does not work
}
template <typename T>
class Test
{
public:
friend void Function() { printf("Function()"); getchar(); }
};
template class Test<int>;
-- end code --
Specifically I receive an error at link time about Function() not
being defined, which I guess is due to the compiler not considering
the definition of Function to be a non-template function and instead
assuming it to be a template function just because it's defined inside
of a template class.
Now, the funny thing is that if you move the main() BELOW the explicit
instantiation of the template class Test, this code actually compile
and works, that's why I consider it to be a "bug" because
theoretically if the definition of Function() is a template function
(thus not a normal function) it should never be used for the call
inside of the main() function, whether the main() function is below
the instantiation or not. Am I right?
Anyway I'm searching for a way to make the definition of Function()
available for the main() function even if the main() function is above
the template instantiation. Is there a way to accomplish this for gcc?
Like some command-line flag or something? I'd really appreciate that.
Thanks.