小编Dra*_*neg的帖子

C++ 模板可以用于条件代码包含吗?

以便:

template <bool Mode>
void doIt()
{
   //many lines
   template_if(Mode)
   {
      doSomething(); // and not waste resources on if
   }
   //many other lines
}
Run Code Online (Sandbox Code Playgroud)

我知道有 enable_if 命令可用于有条件地启用该功能,但我认为我不能在这里使用它这样的选项。基本上我需要的是充当#ifdef 宏的模板构造。

c++ templates

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

在C++中 - 是否可以将volatile shared_ptr与nullptr进行比较?

似乎实现中的volatile比较函数shared_ptr不存在.

存在甚至有意义吗?

c++ volatile shared-ptr

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

TCL中的求反运算

我还没有找到如何在 TCL 中否定布尔值...我尝试过:

set x true
puts !$x   #prints '!true'
puts ![$x] #prints !
puts [!$x] #prints no event matches "true"
puts !{$x} #prints !{true}
puts {!$x} #prints !$x
Run Code Online (Sandbox Code Playgroud)

boolean tcl negation

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

在 C++ 流中使用三元运算符可以吗?

下面的代码:

#include <iostream>
using namespace std;
struct A{int number=10;};

int main()
{
   A* a = new A();
   cout<<"A val: "<< a==nullptr?"":a->number<<endl;
}
Run Code Online (Sandbox Code Playgroud)

使用 c++11 在 gcc 4.7 上编译给出:

错误:“int”和“”类型的操作数对二进制“operator <<”无效

我不明白为什么,正确的方法是什么?我希望空检查尽可能短,因为我希望它们很常见。

cout ternary-operator c++11

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

Mac OS X 上的 darwinssl 库位置是什么?

我正在尝试编译 curl 并将其静态链接到另一个项目。我确实设法为我的 mac 架构(x86_64)编译了它并得到了“libcurl.a”。现在,当我尝试构建时:

#include <curl/curl.h>

int main()
{
  CURL *curl = curl_easy_init();
}
Run Code Online (Sandbox Code Playgroud)

使用 g++ tornado.cc -o tornado (path-to-where-it-is)libcurl.a -std=c++11

我得到:

Undefined symbols for architecture x86_64:
  "_CFArrayAppendValue", referenced from:
      _append_cert_to_array in libcurl.a(libcurl_la-darwinssl.o)
  "_CFArrayCreate", referenced from:
      _darwinssl_connect_common in libcurl.a(libcurl_la-darwinssl.o)
  "_CFArrayCreateMutable", referenced from:
      _darwinssl_connect_step2 in libcurl.a(libcurl_la-darwinssl.o)
  "_CFArrayGetCount", referenced from:
      _darwinssl_connect_common in libcurl.a(libcurl_la-darwinssl.o)
  "_CFArrayGetValueAtIndex", referenced from:
      _darwinssl_connect_common in libcurl.a(libcurl_la-darwinssl.o)
  "_CFDataCreate", referenced from:
      _append_cert_to_array in libcurl.a(libcurl_la-darwinssl.o)
Run Code Online (Sandbox Code Playgroud)

据我了解,它想获取 darwinssl 的链接,但我不知道从哪里获取这些链接。

curl 的自述文件(https://github.com/biasedbit/curl-ios-build-scripts/blob/master/README.md) - 确实提到它取决于 libz.dylib 和 Security.framework 但没有提供任何线索关于它们是什么,我何时以及如何需要它们,以及如何包含它们。

PS常规卷曲包含工作正常: g++ tornado.cc …

c++ macos curl darwin

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

非类型模板参数不引用任何声明?

#include <iostream>                                                                                                               
#include <string>                                                                                                                 

using namespace std;                                                                                                              

template <typename T,                                                                                                             
          typename T::type N,                                                                                                     
          typename T::strata& X>                                                                                                  
struct SomeClass{};                                                                                                               

struct S1                                                                                                                         
{                                                                                                                                 
  typedef int type;                                                                                                               
  typedef string strata;                                                                                                          
};                                                                                                                                

int main () {                                                                                                                     
  SomeClass<S1, 3, string("erg")> x;                                                                                              
}    
Run Code Online (Sandbox Code Playgroud)

失败并显示消息:

 g++ templ.cc -o templ -std=c++14                                                                                   
 templ.cc:18:20: error: non-type template argument does not refer to any declaration                                               
   SomeClass<S1, 3, string("erg")> x;                                                                                              
                    ^~~~~~~~~~~~~                                                                                                  
 templ.cc:8:24: note: template parameter is declared here                                                                          
           typename T::strata& X> 
Run Code Online (Sandbox Code Playgroud)

为什么它适用于 int 而不适用于字符串?为什么它说字符串是非类型参数?

c++ templates

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

无法将std :: cout传递给ostream和构造函数

我试图创建通用的流类持有者,但似乎我无法传递std::cout给它,代码:

#include <iostream>

struct x
{
    std::ostream &o;
    x(std::ostream &o):o(o){}
};

int main()
{
    x(std::cout);
    x.o<<"Hi\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译时失败:

c++ str.cc -o str -std=c++11
str.cc: In function ‘int main()’:
str.cc:11:14: error: invalid use of qualified-name ‘std::cout’
str.cc:12:4: error: expected unqualified-id before ‘.’ token
Run Code Online (Sandbox Code Playgroud)

为什么?

c++ cout

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

C++中是否存在'override'说明符的反转?

override关键字允许确保该函数将被覆盖.我正在寻找相反的功能.所以 - 当我写一个新函数时 - 我想用它来标记它以确保它不会被意外覆盖.

(另外,我不想做它static,因为它看起来像属于一个对象而不是类)

c++ inheritance overriding c++11

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

如何将lambda传递给lambda?

我还没有找到为什么这段代码不起作用:

#include <iostream>
#include <functional>

using namespace std;

int main()
{
  auto xClosure = [](const function<void(int&)>& myFunction) {
    myFunction(10);};

  xClosure([]
       (int& number) -> void
       {cout<<number<<endl;
       });
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它返回:

g++ test.cc -o test -std=c++14
Run Code Online (Sandbox Code Playgroud)
 test.cc:9:5: error: no matching function for call to object of type 'const function<void
  (int &)>'
Run Code Online (Sandbox Code Playgroud)

c++ lambda c++11

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