小编lyt*_*nyn的帖子

具有类参数的函数是否从命名空间泄露?

我在这里有一小段代码供您考虑,这让我感到非常困惑.奇怪的是,它在Sun Studio和GCC上编译,即使我认为它不应该.

考虑一下:

namespace name
{
  class C
    {
      int a;
    };

  void f(C c);
  void g(int a);
}

int main(int argc, char** argv)
{
  name::C c;

  name::f(c); 
  f(c);  // <--- this compiles, strangely enough

  name::g(42);
  // g(42);  <--- this does not, as I expected
}
Run Code Online (Sandbox Code Playgroud)

来自同一命名空间的类参数会导致函数f"泄漏"到命名空间之外,并且无需访问name::.

有人对此有解释吗?这当然是我而不是编译器在这里错了.

c++ namespaces argument-dependent-lookup

22
推荐指数
3
解决办法
943
查看次数

boost :: shared_ptr <T>和boost :: shared_ptr <const T>共享引用计数吗?

关于boost::shared_ptrs的陷阱有几个有趣的问题.在其中一个中,有一个有用的提示,以避免指向boost::shared_ptr<Base>boost::shared_ptr<Derived>相同的类型对象,Derived因为它们使用不同的引用计数,并可能过早地破坏对象.

我的问题:是否安全兼得boost::shared_ptr<T>boost::shared_ptr<const T>点型的同一个对象T,还是将导致同样的问题?

c++ boost smart-pointers shared-ptr

12
推荐指数
1
解决办法
2323
查看次数

将仅标头模板库编译到共享库中?

我们正在设计一个新的C++库,并决定采用基于模板的方法以及针对极端情况的一些特定的部分模板特化.特别是,这将是一个仅标题模板库.

现在,有人担心这会导致二进制文件中的大量代码重复,因为这个模板"库"将被编译到任何其他共享库或使用它的可执行文件中(可以说只是那些使用的部分).我仍然认为这不是一个问题(特别是,编译器甚至可以内联它不能跨共享库边界的东西).

但是,既然我们知道将要使用的有限类型的集合, 有没有办法将这个头编译成一个库,并提供一个只有声明而不是其他的不同的头?请注意,库不仅必须包含通用实现,还必须包含部分特化.

c++ templates shared-libraries

9
推荐指数
1
解决办法
5817
查看次数

C++输入操作符重载">>"

我有一个有理数的类是由两个整数组成的:num提名者和den分母.

以下运算符应该从流中读取有理数.

istream& operator >> (istream& Is, rational& r) {
  char c; //Test char.
   double n; //Could be either the numerator of the fraction or the antiperiod of the repeating decimal number.
    Is >> n;
    int i = 0;
    for (; n*10-pow(10, i+1) < 1 && int(n) != 0; i++) {
        n *= 10;
    }
    for (; int(n*10) % 10; i++) {
        n *= 10;
    }
    n /= pow(10, i);
    if (i == 0) {
        r.num …
Run Code Online (Sandbox Code Playgroud)

c++ input operator-overloading cin rational-numbers

6
推荐指数
1
解决办法
2390
查看次数

Segfault使用变换指针向抽象类的向量

我遇到以下代码的段错误:

我有一个A带方法的抽象类

virtual bool Ok() const;
Run Code Online (Sandbox Code Playgroud)

现在,我有以下向量

std::vector<A*> v;
Run Code Online (Sandbox Code Playgroud)

填充了几个指向现有子对象的指针.我想积累Ok()方法的结果如下:

std::vector<bool> results;
std::transform(v.begin(), v.end(), results.begin(), std::mem_fun(&A::Ok));
std::accumulate(results.begin(), results.end(), true, std::logical_and<bool>());
Run Code Online (Sandbox Code Playgroud)

不幸的是,我总是在第二行遇到段错误,我不明白为什么.通过标准C++循环替换转换调用可修复segfault.有任何想法吗?

c++ transform

1
推荐指数
1
解决办法
671
查看次数

solaris上的std :: sort问题(libCstd)

我在使用Sun Studio编译器的Solaris上遇到问题,这很可能是由于使用了奇怪的STL实现(libCstd),请参阅http://developers.sun.com/solaris/articles/cmp_stlport_libCstd.html.考虑一下:

std::vector<C*> v;
// .. populate the vector
std::sort(v.begin(), v.end());
Run Code Online (Sandbox Code Playgroud)

C某个班级在哪里.这会产生以下编译器错误消息:

"/opt/sunstudio12.1/prod/include/CC/Cstd/./algorithm", line 725: Error: The operand "*first" cannot be assigned to.
"/opt/sunstudio12.1/prod/include/CC/Cstd/./algorithm.cc", line 985:     Where: While instantiating "std::__linear_insert<C*const*, C*>(C*const*, C*const*, C**)".
"/opt/sunstudio12.1/prod/include/CC/Cstd/./algorithm.cc", line 985:     Where: Instantiated from std::__insertion_sort<C*const*>(C*const*, C*const*).
"/opt/sunstudio12.1/prod/include/CC/Cstd/./algorithm", line 811:     Where: Instantiated from non-template code.
Run Code Online (Sandbox Code Playgroud)

有人知道如何规避这个问题吗?当然,实际上我想使用std::sort自定义比较仿函数,但即使是这个简单的版本也行不通.

c++ solaris stl sunstudio

1
推荐指数
1
解决办法
1609
查看次数