Neelesh Bodas wrote:
Hello all,
Is there a way to pass an object of local type to a template?
template <class T> int fun(const T& x) { return 1; }
int bar()
{
enum Color { Red, Green, Blue};
Color c;
fun(c); // Error, Color is local to bar()
}
No. The usual suggestion is to place the declarations in an anonymous
namespace:
template <class T> int fun(const T& x) { return 1; }
namespace {
enum Color { Red, Green, Blue};
}
int main()
{
Color c;
fun(c); // OK, Color is unique to local file
}
An anonymous namespace essentially allows local types at the file level
to be used as template type parameters.
Greg