Boost logo

Boost Users :

Subject: Re: [Boost-users] [SmartPtr] shared_ptr as a default value in function template
From: Pekka Seppänen (pekka.seppanen_at_[hidden])
Date: 2013-05-23 06:54:17


On 23.5.2013 13:08, Igor R wrote:
> Hello,
>
> In the wake of one of recent SO posts... The following doesn't compile
> in MSVC10 (Boost 1.53):
>
> #include <boost/shared_ptr.hpp>
> #include <boost/make_shared.hpp>
>
> template<class T>
> void f(T item, boost::shared_ptr<int> name =oost::make_shared<int>())
> {
> }
>
> int main()
> {
> f(0);
> }
>

Interesting.. but:

It seems to be a MSVC related issue; Unless the templated function
(make_shared in this case) is in the same namespace as the called templated
function (f in this case) MSVC throws an error.

Place both under the same namespace and the error disappears. Say using
namespace and the problem disappears.

Google revealed this:
http://stackoverflow.com/questions/2788765/stdmake-shared-as-a-default-argument-does-not-compile

Unfortunaly, the Microsoft Connect link is dead so I don't know if this was
ever fixed. MSVC8 has this problem too (and it wasn't ever fixed for that
version).

Here's a small test case. Doesn't compile on MSVC8 but no problems whatsover
with GCC 4.4.5.

template < typename T >
T same_ns() { return 42; }

namespace foo
{
template < typename T >
T diff_ns() { return 42; }
}

template < typename T >
void f(int answer =ame_ns< int >()) {}

// workaround
using namespace foo;

template < typename T >
void u(int answer =iff_ns< int >()) {} // compiles

template < typename T >
void p(int answer =oo::diff_ns< int >()) {} // does not compile on msvc

int main()
{
    f< int >();
    u< int >();
    p< int >();
    return 0;
}

-- Pekka


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net