Templates, Boost tuples & Three compilers

A

Alex Vinokur

Hi,

Here is some program.

It was compiled with three compilers.


BOOST Version: 1_34

--- HP-UX, aCC ---
aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]
aCC +DD64 -AA -I/adjhome/ip/ccadj/ccadj/BOOST/boost_1_34_0 prog1.cpp
// No errors


--- AIX, xlC ---
IBM XL C/C++ Enterprise Edition V8.0 for AIX

xlC_r -q64 -qwarn64 -I/usr/local64/boost_1_34_0 prog1.cpp
// No errors


--- Linux, icpc ---
Intel(R) C++ Intel(R) 64 Compiler Professional for applications
running on Intel(R) 64, Version 11.0
icpc -I/adjhome/adj/ccadj/ccadj/BOOST/boost_1_34_0 prog1.cpp
prog1.cpp(47): error: expected an expression
v.push_back (t.get<I>());
^

prog1.cpp(62): error: expected an expression
v.push_back (t.get<0>());
^
detected during instantiation of
"std::vector<boost::tuples::element<0, T>::type,
std::allocator<boost::tuples::element<0, T>::type>> Foo<T,
I>::tuple2vector(const T &) [with T=boost::tuples::tuple<int, int,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type>, I=1UL]" at line
80

compilation aborted for prog1.cpp (code 2)



--------------
Something is wrong?

Thanks.

Alex Vinokur


========= prog1.cpp =========
#include <iostream>
#include <vector>
#include <boost/static_assert.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple_io.hpp>

// ----------------------------------------
template<std::size_t N, typename T> struct uniform_tuple;

template<typename T>
struct uniform_tuple<1,T>
{
typedef typename boost::tuple<T> type;
};

template<typename T>
struct uniform_tuple<2,T>
{
typedef typename boost::tuple<T, T> type;
};


template<typename T>
struct uniform_tuple<3,T>
{
typedef typename boost::tuple<T, T, T> type;
};

template<typename T>
struct uniform_tuple<4,T>
{
typedef typename boost::tuple<T, T, T, T> type;
};


// --------------------------------------
template <class T, std::size_t I>
class Foo
{
BOOST_STATIC_ASSERT (I < boost::tuples::length<T>::value);
public:
typedef typename boost::tuples::element<0,T>::type LocalType;
static std::vector<LocalType> tuple2vector (const T& t)
{
std::vector<LocalType> v = Foo<T, I-1>::tuple2vector(t);
v.push_back (t.get<I>());
return v;
}

};


template <class T>
class Foo<T, 0>
{
public:
typedef typename boost::tuples::element<0,T>::type LocalType;
static std::vector<LocalType> tuple2vector (const T& t)
{
std::vector<LocalType> v;
v.push_back (t.get<0>());
return v;
}

};

// --------------------
template <typename T>
class DoIt : public Foo<T, boost::tuples::length<T>::value - 1>
{
};



int main(void)
{
typedef uniform_tuple<2, int>::type Tuple2;
std::vector<int> v;
v = DoIt<Tuple2>::tuple2vector(boost::tuples::make_tuple (100,
200));

for (std::size_t i = 0; i < v.size(); i++)
{
std::cout << v << " ";
}
std::cout << std::endl;

return 0;
}

=============================
 
B

Bart van Ingen Schenau

Hi,

Here is some program.

It was compiled with three compilers.

BOOST Version: 1_34

--- HP-UX, aCC ---
aCC: HP C/aC++ B3910B A.06.15 [May 16 2007]
aCC +DD64 -AA -I/adjhome/ip/ccadj/ccadj/BOOST/boost_1_34_0 prog1.cpp

// No errors

--- AIX, xlC ---
IBM XL C/C++ Enterprise Edition V8.0 for AIX

xlC_r -q64 -qwarn64 -I/usr/local64/boost_1_34_0 prog1.cpp
// No errors

--- Linux, icpc ---
Intel(R) C++ Intel(R) 64 Compiler Professional for applications
running on Intel(R) 64, Version 11.0
icpc -I/adjhome/adj/ccadj/ccadj/BOOST/boost_1_34_0 prog1.cpp

prog1.cpp(47): error: expected an expression
      v.push_back (t.get<I>());
                            ^

prog1.cpp(62): error: expected an expression
      v.push_back (t.get<0>());
                            ^
          detected during instantiation of
"std::vector<boost::tuples::element<0, T>::type,
std::allocator<boost::tuples::element<0, T>::type>> Foo<T,
I>::tuple2vector(const T &) [with T=boost::tuples::tuple<int, int,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type>, I=1UL]" at line
80

compilation aborted for prog1.cpp (code 2)

Yes, something is wrong in the code.
Thanks.

Alex Vinokur

========= prog1.cpp =========
// --------------------------------------
template <class T, std::size_t I>
class Foo
{
  BOOST_STATIC_ASSERT (I < boost::tuples::length<T>::value);
public:
  typedef typename boost::tuples::element<0,T>::type LocalType;
  static std::vector<LocalType> tuple2vector (const T& t)
  {
    std::vector<LocalType> v = Foo<T, I-1>::tuple2vector(t);
    v.push_back (t.get<I>());
^^^^^^^^^^
As t is a dependent name, the compiler may (should) need some
assistence here to realise you are using a template member of t:
v.push_back (t.template get said:
    return v;
  }

};

template <class T>
class Foo<T, 0>
{
public:
  typedef typename boost::tuples::element<0,T>::type LocalType;
  static std::vector<LocalType> tuple2vector (const T& t)
  {
    std::vector<LocalType> v;
    v.push_back (t.get<0>());

Same here.
    return v;
  }

};
=============================

Bart v Ingen Schenau
 
A

Alex Vinokur

On Jul 17, 11:43 am, Bart van Ingen Schenau <[email protected]>
wrote:
[snip]
As t is a dependent name, the compiler may (should) need some
assistence here to realise you are using a template member of t:
  v.push_back (t.template get<I>());
[snip]

It works fine.

Thanks.

Alex Vinokur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,757
Messages
2,569,543
Members
45,026
Latest member
camilin05

Latest Threads

Top