Boost logo

Boost Users :

Subject: Re: [Boost-users] boost 1.37, tr1 and gcc 4.x
From: Sergey Sadovnikov (flex_ferrum_at_[hidden])
Date: 2008-11-11 11:38:46


Hello, liam.

Tuesday, November 11, 2008 at 6:53:28 PM you wrote:

lm> I would say that is not ugly but good practice ie "do not pollute the
lm> global namespace"

Ok. Let's take the boost example:
http://www.boost.org/doc/libs/1_37_0/libs/bind/bind_as_compose.cpp
, try to compile it with some modifications:

#include <boost/config.hpp>

#if defined(BOOST_MSVC)
#pragma warning(disable: 4786) // identifier truncated in debug info
#pragma warning(disable: 4710) // function not inlined
#pragma warning(disable: 4711) // function selected for automatic inline expansion
#pragma warning(disable: 4514) // unreferenced inline removed
#endif

//
// bind_as_compose.cpp - function composition using bind.hpp
//
// Version 1.00.0001 (2001-08-30)
//
// Copyright (c) 2001 Peter Dimov and Multi Media Ltd.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#include <tr1/functional>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/function.hpp>

std::string f(std::string const & x)
{
    return "f(" + x + ")";
}

std::string g(std::string const & x)
{
    return "g(" + x + ")";
}

std::string h(std::string const & x, std::string const & y)
{
    return "h(" + x + ", " + y + ")";
}

std::string k()
{
    return "k()";
}

template<class F> void test(F f)
{
    std::cout << f("x", "y") << '\n';
}

int main()
{
    using namespace boost;
    using namespace std;
    using namespace tr1;

    function<void()> foo;

    // compose_f_gx

    test( bind(f, bind(g, _1)) );

    // compose_f_hxy

    test( bind(f, bind(h, _1, _2)) );

    // compose_h_fx_gx

    test( bind(h, bind(f, _1), bind(g, _1)) );

    // compose_h_fx_gy

    test( bind(h, bind(f, _1), bind(g, _2)) );

    // compose_f_k

    test( bind(f, bind(k)) );

    return 0;
}

and face out following errors:
g++ -std=c++0x -IP:\projects\common\boost_1.36.0 boost_test.cpp
boost_test.cpp: In function 'int main()':
boost_test.cpp:60: error: reference to 'function' is ambiguous
cc1plus.exe: error: candidates are: #'tree_list' not supported by dump_decl#<declaration error>
P:\projects\common\boost_1.36.0/boost/function/function_base.hpp:99: error: template<class Signature> struct boost::function
boost_test.cpp:60: error: 'foo' was not declared in this scope

Compiler is gcc 4.3.x.

As you can see:
1. Global namespace was not polluted. Namespaces exposed only into the 'main'
function scope.
2. Code is fully conform to the boost documentation.

-- 
Best Regards,
 Sergey                          mailto:flex_ferrum_at_[hidden]

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