CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    [RESOLVED] Template Cast Operator. Possible?

    I'm trying to do something in my code and it's giving me an error on compile. What I'm trying to do is put a template cast operator in a container class. Here's the simplified relevant code:
    Code:
    template<class T> class Container
    {
    public:
        template<class U> Container<U> operator U  (void)
        {
            // Convert each T element in 'list' to U elements in new Container, then return the container
        }
    protected:
        std::vector<T> list;   // Contained objects
    };
    The operator method is actually defined in a separate file, if that matters.

    Following is the GCC error given:
    error: operator 'U' declared to return 'Container<U>'

    I don't even know if this is possible. I need to know whether or not it is possible, and if so then how. What am I doing wrong? Otherwise, if anyone could come up with an alternative way to do this, I would greatly appreciate it. And if you're wondering, yes I have a good reason for wanting to do this. Thanks.
    Last edited by Etherous; March 20th, 2009 at 07:10 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured