Boost logo

Boost :

Subject: [boost] [concept] forward declarations and `BOOST_CONCEPT_REQUIRES()`
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2010-08-09 13:03:42


Hello all,

Can I forward declare a function template that checks concepts using
`BOOST_CONCEPT_REQUIRES()`?

In the following example, MSVC gives me an ambiguous call error when I
try to do that... (I have not yet tried other compilers).

Example "c00.cpp":

    #include <boost/concept/requires.hpp>
    #include <boost/concept_check.hpp>
    #include <vector>

    // (1) Forward declaration with concepts.
    template<class Iter, class T>
        BOOST_CONCEPT_REQUIRES(
            ((boost::ForwardIterator<Iter>)) ((boost::EqualityComparable<T>))
        ,
    (bool) ) all_equals(Iter first, Iter last, const T& val);

    // (2) Actual definition.
    template<class Iter, class T>
        BOOST_CONCEPT_REQUIRES(
            ((boost::ForwardIterator<Iter>)) ((boost::EqualityComparable<T>))
        ,
    (bool) ) all_equals(Iter first, Iter last, const T& val) {
        for (Iter i = first; i < last; ++i) {
            if (*i != val) return false;
        }
        return true;
    }

    int main() {
        std::vector<double> v;
        // MSVC error: Cannot resolved ambiguous call between (1) and (2).
        all_equals(v.begin(), v.end(), double());
        return 0;
    }

MSVC error:

    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version
14.00.50727.42 for 80x86
    Copyright (C) Microsoft Corporation. All rights reserved.

    c00.cpp
    c00.cpp(28) : error C2668: 'all_equals' : ambiguous call to
overloaded function
            c00.cpp(18): could be 'bool
all_equals<std::_Vector_iterator<_Ty,_Alloc>,double>(Iter,Iter,const T
&)'
            with
            [
                _Ty=double,
                _Alloc=std::allocator<double>,
                Iter=std::_Vector_iterator<double,std::allocator<double>>,
                T=double
            ]
            c00.cpp(11): or 'bool
all_equals<std::_Vector_iterator<_Ty,_Alloc>,double>(Iter,Iter,const T
&)'
            with
            [
                _Ty=double,
                _Alloc=std::allocator<double>,
                Iter=std::_Vector_iterator<double,std::allocator<double>>,
                T=double
            ]
            while trying to match the argument list
'(std::_Vector_iterator<_Ty,_Alloc>,
std::_Vector_iterator<_Ty,_Alloc>, double)'
            with
            [
                _Ty=double,
                _Alloc=std::allocator<double>
            ]

Thank you very much.

-- 
Lorenzo

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