小编Dan*_*rey的帖子

虽然包含<typeinfo>,但Clang拒绝type_info为不完整

我很遗憾为什么Clang拒绝以下代码:

#include <typeinfo>
#include <exception>

const char* get_name( const std::exception_ptr eptr )
{
  return eptr.__cxa_exception_type()->name();
}

int main() {}
Run Code Online (Sandbox Code Playgroud)

GCC没问题,但Clang抱怨说它type_info是一个不完整的类型:

$ g++-4.7 -std=c++0x -O3 -Wall -Wextra t.cc -o t
$ clang++-3.2 -std=c++0x -O3 -Wall -Wextra t.cc -o t
t.cc:6:37: error: member access into incomplete type 'const class type_info'
  return eptr.__cxa_exception_type()->name();
                                    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/exception_ptr.h:144:19: note: forward declaration of
      'std::__exception_ptr::type_info'
      const class type_info*
                  ^
1 error generated.
$ 
Run Code Online (Sandbox Code Playgroud)

问题:如何使用Clang修复它?或者我错过了什么,Clang拒绝代码是对的吗?

c++ clang libstdc++ c++11

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

OCIErrorGet和OCI_ERROR的多个错误处理

因为OCIErrorGet()它记录了它可能返回多个错误,我用它来提取以下方法OCI_SUCCESS_WITH_INFO,但目前不适用于OCI_ERROR:

void check_error( sword status )
{
    switch( status ) {
    case OCI_SUCCESS:
        break;

    case OCI_SUCCESS_WITH_INFO:
        {
           ub4 recordno = 1;
           while( status != OCI_NO_DATA ) {
              sb4 errcode = 0;
              text errbuf[ 1024 ];
              status = ::OCIErrorGet( m_err, recordno, (text*)NULL, &errcode, errbuf, sizeof( errbuf ), OCI_HTYPE_ERROR );
              if( status == OCI_SUCCESS ) {
                 std::cout << "oracle info: " << (const char*)errbuf << std::endl;
              }
              else {
                 assert( status == OCI_NO_DATA );
              }
              ++recordno; …
Run Code Online (Sandbox Code Playgroud)

c++ oracle oracle-call-interface

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

为什么std :: stoi和std :: array没有用g ++ c ++ 11编译?

在过去的几个月里,我一直在学习C++和使用终端.我的代码使用g ++和C++ 11编译并运行良好,但在最近几天它开始出错并且我在编译时遇到了问题.我可以编译和运行的唯一程序取决于较旧的C++标准.

我首先得到的错误与头文件中的#include <array>有关.不知道为什么会这样,但我通过使用boost/array来解决它.我无法解决的另一个错误是使用std :: stoi.array和stoi都应该在C++ 11标准库中.我做了以下简单的代码来演示正在发生的事情:

//
//  stoi_test.cpp
//
//  Created by ecg
//

#include <iostream>
#include <string> // stoi should be in here

int main() {

    std::string test = "12345";
    int myint = std::stoi(test); // using stoi, specifying in standard library
    std::cout << myint << '\n'; // printing the integer

    return(0);

}
Run Code Online (Sandbox Code Playgroud)

尝试使用ecg $ g ++编译-o stoi_trial stoi_trial.cpp -std = c ++ 11

array.cpp:13:22:错误:命名空间'std'中没有名为'stoi'的成员; 你是说'ati'吗?int myint = std :: stoi(test); ~~~~~ ^ ~~~ atoi /usr/include/stdlib.h:149:6:注意:'atoi'在这里声明为int atoi(con​​st …

c++ macos boost g++ c++11

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

在C++中声明一个operator +函数

我有一个班级桑普.在Samp.cpp中,我可以定义/声明一个类似的函数

 Samp& operator+(Samp& other) {
  std::cout << "something";
  return other;
}
Run Code Online (Sandbox Code Playgroud)

这个功能究竟是什么?我怎么称呼它?

c++ operator-overloading

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

左值初始化失败

"C++编程语言第4版"中的一些示例代码让我感到困惑.这是我的测试用例.

Env gcc版本4.6.3(Debian 4.6.3-14 + rpi1),其中-std = c ++ 0x

  • Code1 string var {"Cambridge"}; string& r1 {var}; 编译失败
  • Code2 string var {"Cambridge"}; string& r1 = var; 编译成功
  • Code3 string var {"Cambridge"}; string&& r1 {var}; 编译成功
  • Code1编译失败 g++ -g -DLINUX -std=c++0x -c src/dummy.cpp -o src/dummy.o src/dummy.cpp: In function ‘int main(int, char**)’: src/dummy.cpp:26:17: error: invalid initialization of non-const reference of type ‘std::string& {aka std::basic_string<char>&}’ from an rvalue of type ‘<brace-enclosed initializer list>’ make: *** [src/dummy.o] Error 1
  • 根据书籍,Code1应该没问题.第7.7.2节因为var是左值,但为什么code1不起作用,但code3在我的情况下工作?

c++ string gcc c++11

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

(右值引用)VS(const左值引用)作为C++ 11中的函数参数

当作为函数参数工作时,有人能解释何时rvalue引用优于const lvalue引用?

背景:我试图将一个const指针传递给一个函数.由于我必须考虑传入本地指针并传入临时的情况(例如从函数调用返回),我有两个选择:参数可以声明为:

void foo(T const* const&); //const lvalue ref to const ptr
Run Code Online (Sandbox Code Playgroud)

要么

void foo(T const* &&); //rvalue ref to const ptr
Run Code Online (Sandbox Code Playgroud)

但是这个rvalue引用不能绑定到局部变量(它是左值类型.但我确实记得Scott Meyers创造了术语"通用引用"来引用右值引用.这让我更加困惑.)所以我的问题是,因为第一个声明可以处理这两种情况,第二个使用右值引用的时候是首选吗?

注意:在第一种方法中,其他形式

void foo(const const T* &); 
void foo(const T* const&); 
Run Code Online (Sandbox Code Playgroud)

没用.我想原因是在后两者中我在const限定符的位置上并不一致(如果我错了,请纠正我).

c++ rvalue-reference c++11

6
推荐指数
2
解决办法
3185
查看次数

在std :: map中为std :: array分配多个值

使用std :: array时,我可以一次分配值:

std::array<int, 3> a2 = {1, 2, 3}; 
Run Code Online (Sandbox Code Playgroud)

但是当上面的数组合并到一个地图中时,我不知道最好的方法:

using namespace std;
map <string, array<int, 3>> myMap;

//I'm doing it like below now...

array<int, 3> tempArray = {1,2,3}; // can I save this line somehow?
myMap[myString] = tempArray;
Run Code Online (Sandbox Code Playgroud)

如果这确实是正确的方法,也请告诉我.谢谢!

c++ stl map c++11

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

为什么对基本类型的操作是无序的而不是不确定的顺序?

如果i是一个int,则像这样的表达式++i + ++i是未定义的行为,因为有 2 个无序的修改i。然而, ifi是一些int类似的类,++i + ++i而是具有不确定顺序的修改,因此是定义的行为(在这种情况下具有确定性结果)。是否存在对原语的操作不排序而不是不确定排序会更好的情况?如果是这样,为什么这种情况不适用于用户创建的类型?如果不是,为什么原始操作根本没有顺序?

c++ undefined-behavior sequence-points c++11

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

是否将rvalue作为参数传递给函数内的左值?

我有一个View和一个Shape类,其中View"拥有"它的Shape对象.我将其实现为unique_ptr的向量.在函数View :: add_shape(std :: unique_ptr && shape)中,我仍然需要在rvalue参数上使用std :: move来进行编译.为什么?(使用GCC 4.8)

#include <memory>
#include <vector>
using namespace std;

class Shape { };
class View
{
  vector<unique_ptr<Shape>> m_shapes;
  public:
  void add_shape(unique_ptr<Shape>&& shape)
  { 
    m_shapes.push_back(std::move(shape));// won't compile without the std::move
  }
};


int main()
{
  unique_ptr<Shape> ups(new Shape);
  View v;
  v.add_shape(std::move(ups));
}
Run Code Online (Sandbox Code Playgroud)

c++ rvalue c++11

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

使用SDL 2.0和C++ 11进行CMake

我在使用CMake编译C++ 11/SDL 2.0代码时遇到了一些麻烦.CMakeLists非常简单:

cmake_minimum_required(VERSION 2.6)
project(Test)

include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(sdl2test ${SDL2_LIBRARIES})

add_definitions(-std=c++0x)
add_executable(Test src/main.cpp)
Run Code Online (Sandbox Code Playgroud)

但是,在编译时,我收到此错误:

Scanning dependencies of target Test
[100%] Building CXX object CMakeFiles/Test.dir/src/main.cpp.o
/var/dev/cmake/src/main.cpp: In function ‘int main(int, char**)’:
/var/dev/cmake/src/main.cpp:11:16: error: ‘nullptr’ was not declared in this scope
/var/dev/cmake/src/main.cpp:17:16: error: ‘nullptr’ was not declared in this scope
make[2]: *** [CMakeFiles/Test.dir/src/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Test.dir/all] Error 2
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)

cmake版本2.8.9和gcc 4.7.2.有关如何正确使用CMake的C++ 11功能的任何想法?

ps:我也试过-std = c ++ 11

c++ cmake c++11

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