CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  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.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Template Cast Operator. Possible?

    Type conversion functions like that one don't allow a return type to be specified. I'm not sure if it is possible myself, but I know that you can't specify a return type and that's most likely the reason for your error.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

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

    Re: Template Cast Operator. Possible?

    Oh, right! Yeah, I'm an idiot. It will build now, but I won't have a chance to test it for a while. Thanks

    Edit:
    Actually it will still mess up, as it will try to return a 'U' instead of a 'Container<U>'. I've fixed it and it should work now. Here is the modified code (just the operator):
    Code:
        template<class U> operator Container<U>  (void)
        {
            // Convert each T element in 'list' to U elements in new Container, then return the container
        }
    Again, I won't be able to test it, but it seems to work
    Last edited by Etherous; March 20th, 2009 at 10:07 PM.

  4. #4
    Join Date
    Mar 2009
    Posts
    8

    Re: Template Cast Operator. Possible?

    Here is an example of a type conversion function:
    Code:
    template <typename Lo, typename Hi> Lo down_cast (const Hi& value)
    {
        Lo result = Lo (a);
        if  (Hi (result) != value)
            throw exception ("loss of data in down cast");
        return result;
    }
    So in your case you might want to do something like this (untested) code:
    Code:
    template <typename Tout, typename Tin> 
    vector <Tout> ConvertStuff (const vector <Tin>& vIn)
    {
        vector <Tout> vOut;
    
        for (int i = 0; i < vIn.size (); ++i)
        {
            Tout value = vIn [i];
            vOut.push_back (value);
        }
        return vOut;
    }
    As long as you have an assignment operator to convert the Tin to a Tout you should be okay.

  5. #5
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: [RESOLVED] Template Cast Operator. Possible?

    You can use the copy constructor if type OutT has an assignment operator that accepts type InT instead of manual loops. Furthermore, it allows RVO.

    Code:
    template<typename InT, typename OutT>
    vector<OutT> convert( const vector<InT>& op )
    {
       return vector<OutT>( op.begin(), op.end() );
    }
    - Guido

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