Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-02-24 04:12:39


Bronek Kozicki <brok_at_[hidden]> writes:

>> I wonder if that was discussed at the time? Or
>> is there a better way to prevent the problem?
>
> I remember thread "Perfect Forwarding" where David presented clever way
> to detect copy-from--temporary-rvalue, but I do not know if any of his
> ideas are applicable here. It would be sooo much cleaner just to add
> constructor like:
>
> template <typename Container>
> tokenizer(const Container&& c, const TokenizerFunc& f); // make a copy

Actually, if that's the way tokenizer works, you can do it, I'm pretty
sure:

---
#include <vector>
#include <iostream>
template <class TokenizerFunc>
struct tokenizer
{
    struct rvalue
    {
        template <class T>
        rvalue(T const&) {}
    };
    
    // handle lvalues
    template <typename Container>
    explicit tokenizer(Container& c, const TokenizerFunc& f = TokenizerFunc())
    {
        std::cout << "lvalue\n";
    }
    
    // handle rvalues
    explicit tokenizer(rvalue const& rval, const TokenizerFunc& f = TokenizerFunc())
    {
        std::cout << "rvalue\n";
    }
};
int main()
{
    std::vector<int> const x;
    tokenizer<int> z((x));
    tokenizer<int> z2((std::vector<int>()));
}
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk