Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1958282
  • 博文数量: 261
  • 博客积分: 8073
  • 博客等级: 中将
  • 技术积分: 2363
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-10 15:23
文章分类

全部博文(261)

文章存档

2013年(1)

2012年(1)

2011年(50)

2010年(34)

2009年(4)

2008年(17)

2007年(55)

2006年(99)

分类:

2011-02-16 19:42:33

Sun Studio提供了两个标准库的实现 : libCstd和libstlport
 
libCstd没有完全实现C++标准模板库,Sun Studio默认使用的C++库
libstlport较之libCstd更为接近C++标准模板库
 
如果使用默认的libCstd编译google test会报如下错误:
 
"./include/gtest/gtest-param-test.h", line 279: Error: iterator_traits is not a member of std.
"./include/gtest/gtest-param-test.h", line 279: Error: "," expected instead of "<".
"./include/gtest/gtest-param-test.h", line 279: Error: Illegal value for template parameter.
"./include/gtest/gtest-param-test.h", line 279: Error: value_type is not a member of testing::internal::ParamGenerator.
"./include/gtest/gtest-param-test.h", line 279: Error: A declaration does not specify a tag or an identifier.
"./include/gtest/gtest-param-test.h", line 279: Error: Templates can only declare classes or functions.
"./include/gtest/gtest-param-test.h", line 284: Error: A declaration was expected instead of "return".
"./include/gtest/gtest-param-test.h", line 284: Error: ParamType is not defined.
"./include/gtest/gtest-param-test.h", line 285: Error: No direct declarator preceding "(".
"./include/gtest/gtest-param-test.h", line 289: Error: internal is not defined.
"./include/gtest/gtest-param-test.h", line 289: Error: ParamGenerator is not defined.
"./include/gtest/gtest-param-test.h", line 289: Error: A declaration does not specify a tag or an identifier.
"./include/gtest/gtest-param-test.h", line 289: Error: Templates can only declare classes or functions.
"./include/gtest/gtest-param-test.h", line 291: Error: A declaration was expected instead of "}".
"./include/gtest/gtest-param-test.h", line 294: Error: internal is not defined.
"./include/gtest/gtest-param-test.h", line 294: Error: ParamGenerator is not defined.
"./include/gtest/gtest-param-test.h", line 294: Error: A declaration does not specify a tag or an identifier.
"./include/gtest/gtest-param-test.h", line 294: Error: Templates can only declare classes or functions.
"./include/gtest/gtest-param-test.h", line 297: Error: A declaration was expected instead of "}".
Compilation aborted, too many Error messages.
 
需要使用libstlport编译google test
 
./configure CXX=CC CC=cc CXXFLAGS="-library=stlport4"
 
关于两者的具体区别,如少实现了那些模板、那些函数等,请参考
 
《Comparing C++ Standard Libraries libCstd and libstlport 》
 
 
为了方便现全文引用如下:
 
The Sun C++ compiler ships with two libraries that implement the C++ standard library: libCstd and libstlport.
  • libCstd is used by default and is based on Rogue Wave's stdlib 2.1.1.
  • libstlport is based on STLport version 4.5.3 and is used if you specify the compiler option -library=stlport4.
This article discusses the differences between the two libraries and explores the situations in which one library is preferred over the other.

Background

The Sun C++ compiler started shipping with the standard C++ library libCstd in the C++ 5.0 release of December 1998. Some of the newer language features such as member template classes, member template functions and partial specializations were not yet implemented in the compiler even though the C++ standard library uses all these language features. The libCstd headers use macros to enable or disable functionality in the library that rely on these features and the macros were defined to disable the functionality that uses the newer language features.

Later versions of the compiler implemented the new language features. However, in order to maintain binary compatibility with existing object files, the macros in the libCstd headers were not modified. As a result, the latest version of libCstd continues to have some missing functionality and some of the functions accept a different set of arguments than those that are specified in the standard.

The libstlport library was introduced in the C++ 5.4 release and uses all the language features needed by the library. No functionality in the library is suppressed because of any missing language features. As a result, the libstlport library conforms much more closely to the standard than libCstd does.

Disabled Language Features in libCstd

This section lists the different language features that are disabled in libCstd along with the corresponding missing functionality. Small test examples illustrate the missing features. The compiler issues errors for each test case if the default libCstd is used. All the test cases compile without errors if you specify -library=stlport4. Workarounds using the existing libCstd functionality are provided wherever possible.

Note: The following macros defined in the libCstd header stdcomp.h are used to disable functionality:

  • _RWSTD_NO_MEMBER_TEMPLATES
  • _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  • _RWSTD_NO_MEM_CLASS_TEMPLATES
  • _RWSTD_NO_CLASS_PARTIAL_SPEC

Do not redefine any of the macros whose name starts with _RWSTD because doing so can result in a program that won't compile, won't link or won't run correctly. The reason for such problems is that the headers that are seen by a user program must match the headers that were used to build the libCstd that is linked to the program. By redefining the macro you introduce an inconsistency between the way the library was built and the way it's being used.

Language feature: Member Function Templates

The macro _RWSTD_NO_MEMBER_TEMPLATES is defined in libCstd to indicate that member template functions are not supported and the macro _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE is defined to indicate that the compiler does not support the syntax for functions that are templatized only on the return type. As a result, the following functions are missing.

Missing Members of Class complex in

  • template complex& operator= (const complex& rhs);
  • template complex& operator+= (const complex& rhs);
  • template complex& operator-= (const complex& rhs);
  • template complex& operator*= (const complex& rhs);
  • template complex& operator/= (const complex&);

Example

The following test case does not compile with libCstd:

 % cat c1.c
#include
using namespace std;

struct X {
X(double x) { d = x; }
X& operator +=(double y) { d += y; return *this; }
double d;
};

main()
{
const complex d1(1.0, 2.0);
const complex d2(3.0, 4.0);
complex d3;
d3 = d1;
d3 += d2;
}

% CC c1.c
"c1.c", line 15: Error: Cannot assign const std::complex to
std::complex without "std::complex::operator=(const std::complex&)";.
"c1.c", line 16: Error: The operation "std::complex += const
std::complex" is illegal.
2 Error(s) detected.

% CC -library=stlport4 c1.c
%

Workaround

Relpace the lines

 complex d3;
d3 = d1;
d3 += d2;
with
 complex temp = d1;
temp += d2;
complex d3(temp.real(), temp.imag());

Missing Members of Class basic_string in

In the list below, Typ can be one of: int, unsigned int, long, unsigned long, short, unsigned short, char, unsigned char, wchar_t, or bool. An entry containing Typ is actually ten entries with Typ replaced by each of these types in turn.

  • template 
      basic_string (InputIterator, InputIterator, const Allocator&);
  • basic_string (Typ n, charT c, const Allocator& alloc);
  • template
      basic_string& append (InputIterator, InputIterator);
  • basic_string& append (Typ n, charT c);
  • template
      basic_string& assign (InputIterator, InputIterator);
  • basic_string& assign (Typ n, charT c);
  • template
      void insert (iterator, InputIterator, InputIterator);
  • void insert (iterator p, Typ n, charT c);
  • template
      basic_string& __replace_aux (
       iterator first1,
       iterator last1,
       InputIterator first2,
       InputIterator last2);
  • template
      basic_string& replace (iterator, iterator, InputIterator, InputIterator);
  • basic_string& replace (iterator first, iterator last, Typ n, charT c);

Example

 % cat s1.c
#include
#include
using namespace std;

int main()
{
string s1("abcd");
list l1;
l1.push_back('e');
l1.push_back('f');
string s2 = s1.append( l1.begin(), l1.end() );
}

% CC s1.c
"s1.c", line 11: Error: Could not find a match for std::string::append
(std::list::iterator, std::list::iterator) needed in main().
1 Error(s) detected.

Workaround

Relpace the line

 string s2 = s1.append( l1.begin(), l1.end() );
with
 string s2(s1);
for( list::iterator i = l1.begin(); i != l1.end(); ++i )
s2 += *i;

Missing Member of Class pair in

  • template pair(const pair &p);

Example

 % cat p1.c
#include

struct S {
S() { val = 0; }
S(int i) { val = i; }
int val;
};

main()
{
std::pair p1(1, 2);
std::pair p2 = p1;
}

% CC p1.c
"p1.c", line 12: Error: Cannot use std::pair to initialize std::pair.
1 Error(s) detected.

Workaround

Relpace the line

 std::pair p2 = p1;
with
 std::pair p2(p1.first, p1.second);

Missing members of class locale in

  •  // creating locale based on other locales:
    template locale (const locale& other,Facet* f);
  •  // assignment:
    template locale combine(const locale& other);
  •  // sorting of strings:
    template
      bool operator() (const basic_string& s1,
        const basic_string& s2) const;
  •  // facet access:
    template const Facet& use_facet (const locale&);
    template inline bool has_facet (const locale&) throw();

Example

 % cat l1.c
#include
using namespace std;

main()
{
locale loc1("C");
locale loc2("de");
locale loc3 = loc1.combine >(loc2);
const time_get& f = use_facet >(loc1);
}

% CC l1.c
"l1.c", line 8: Error: Unexpected type name "std::numpunct" encountered.
"l1.c", line 8: Error: combine is not a member of std::locale.
"l1.c", line 9: Error: Could not find a match for std::use_facet
(std::locale) needed in main().
3 Error(s) detected.

Missing Members of Class auto_ptr in

  • template  operator auto_ptr_ref();
  • template  operator auto_ptr();
  • template 
      auto_ptr(auto_ptr&);
  • template 
      inline pair get_temporary_buffer (ptrdiff_t len);

Example

 % cat a1.c
#include

struct A {};
struct B : public A {};

main()
{
std::auto_ptr b(new B);
std::auto_ptr a(b);
}

% CC a1.c
"a1.c", line 9: Error: Cannot use std::auto_ptr to initialize std::auto_ptr
.
1 Error(s) detected.

Missing Members of Class bit_set in

  • template 
      basic_string to_string () const;

Example

 % cat b1.c
#include
#include
using namespace std;

int main()
{
bitset<7> b(4);
string s = b.to_string, allocator >();
cout << "s = " << s << endl;
}

% CC b1.c
"b1.c", line 8: Error: Badly formed expression.
"b1.c", line 8: Error: "char_traits" cannot be declared as an object or function.
"b1.c", line 8: Error: "allocator" cannot be declared as an object or function.
"b1.c", line 8: Error: "," expected instead of ">".
4 Error(s) detected.

Workaround

Replace the line

 string s = b.to_string, allocator >(); 
with
 string s = b.to_string();.

Language Feature: Member Template Classes

The macro _RWSTD_NO_MEM_CLASS_TEMPLATES is defined to indicate that the compiler does not support member template classes. As a result, the following class and constructor are not defined in :

  • template class auto_ptr_ref{};
  • auto_ptr(auto_ptr_ref&);

Example

 % cat a2.c
#include
using namespace std;

class A {};
class B : public A {};

main()
{
auto_ptr_ref
(auto_ptr::*pf)() =
&auto_ptr:: operator auto_ptr_ref
;
}

% CC a2.c
"a2.c", line 9: Error: auto_ptr_ref is not defined.
"a2.c", line 9: Error: Unexpected type name "A" encountered.
"a2.c", line 9: Error: Identifier expected instead of "*".
"a2.c", line 9: Error: pf is not defined.
"a2.c", line 10: Error: An overloadable operator was expected instead of "auto_ptr_ref".
"a2.c", line 10: Error: auto_ptr_ref is not a member of std::auto_ptr.
"a2.c", line 10: Error: Unexpected type name "A" encountered.
"a2.c", line 10: Error: Operand expected instead of ";".
8 Error(s) detected.

Language Feature: Partial Specializations

The macro _RWSTD_NO_CLASS_PARTIAL_SPEC is defined to indicate that the compiler does not support partial specialization of template classes with default parameters. As a result, the following global functions are missing in .

  • count()
  • count_if()

Example

 % cat c2.c
#include
#include
#include
#include
using namespace std;

bool gtr3(int n)
{
return (n > 3);
}

main()
{
// count elements in a list
list x;

for (int i = 0; i < 10; i++)
x.push_back(i);

cout << "list: ";
copy (x.begin(), x.end(), ostream_iterator(cout, " "));
cout << endl;

int n1 = count(x.begin(), x.end(), 6); // # elements with value 6
int n2 = count_if(x.begin(), x.end(), gtr3); // # elements with value >3

cout << "n1 = " << n1 << endl;
cout << "n2 = " << n2 << endl;
}

% CC c2.c
"c2.c", line 23: Error: Could not find a match for std::count
(std::list::iterator, std::list::iterator, int) needed in main().
"c2.c", line 24: Error: Could not find a match for std::count_if
(std::list::iterator, std::list::iterator, bool(int)) needed in main().
2 Error(s) detected.

Workaround

Relpace the lines

 int n1 = count(x.begin(), x.end(), 6); // # elements with value 6
int n2 = count_if(x.begin(), x.end(), gtr3); // # elements with value >3
with
 int n1 = 0;
int n2 = 0;
count(x.begin(), x.end(), 6, n1); // # elements with value 6
count_if(x.begin(), x.end(), gtr3, n2); // # elements with value >3

Missing Template Classes in

  • template struct iterator_traits {}
  • template struct iterator_traits {}
  • template struct iterator_traits {}

Example

 % cat i1.c
#include
#include
#include
using namespace std;

main()
{
vector v(10);

for (int i = 0; i < 10; i++)
v[i] = i;

iterator_traits::value_type x = v[5];
cout << "vector: ";
copy (v.begin(), v.end(), ostream_iterator(cout, " "));
cout << endl;
cout << "x = " << x << endl;
}

% CC i1.c
"i1.c", line 13: Error: iterator_traits is not defined.
"i1.c", line 13: Error: Badly formed expression.
"i1.c", line 17: Error: x is not defined.
3 Error(s) detected.

Workaround

Relpace the line

 iterator_traits::value_type x = v[5];
with
 int x = v[5];

Which Library Should You Use?

The Sun C++ compiler uses libCstd by default. If binary compatibility with existing object files is required, or if the application links with several other libraries that were built using libCstd then libCstd should be used for building the application. The drawback is that the application cannot use the features that are missing in libCstd.

libstlport provides an implementation that is much closer to the standard. If an application needs the functionality that is missing in libCstd then libstlport can be used. However, note that both libCstd and libstlport cannot be used in the same application. If the application is built with -library=stlport4 then all libraries that are linked with the application and that use the standard C++ library functions should also be built with -library=stlport4. This may not always be possible if some third party libraries are used. The user may have to ask the third party vendors to provide libraries built with -library=stlport4.

Another reason to use libstlport may be for better performance. Some of the classes in libstlport have a better performance compared to libCstd. Since the performance of an application depends on several factors, it may be best to try using each library in turn and measure the performance and then use the library that provides the best results.

Summary

  • libCstd does not support all the features of the standard library.
  • libstlport conforms more closely to the C++ standard.
  • Use libCstd if you desire binary compatibility with existing object files.
  • Use libstlport if your application needs the missing functionality of libCstd.
  • In some cases, libstlport may provide better performance.
 
 
阅读(1761) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~