为什么下面的代码没有编译?
// source.cpp
int main()
{
constexpr bool result = (0 == ("abcde"+1));
}
Run Code Online (Sandbox Code Playgroud)
编译命令:
$ g++ -std=c++14 -c source.cpp
Run Code Online (Sandbox Code Playgroud)
输出:
source.cpp: In function ‘int main()’:
source.cpp:4:32: error: ‘((((const char*)"abcde") + 1u) == 0u)’ is not a constant expression
constexpr bool result = (0 == ("abcde"+1));
~~~^~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我正在使用gcc6.4.
我必须测试一个提供自己的内存分配例程的库:
void* allocation_routine(size_t size) throw();
Run Code Online (Sandbox Code Playgroud)
文档指出该函数至少分配大小字节的内存(允许分配更多字节)。顺便说一下,该函数在内部使用posix_memalign,但是实现可能会发生变化。
我想知道是否可以为这种功能编写单元测试?我们如何测试是否分配了所需的内存量?
更新:
如果我们不能编写单元测试,那么最接近的解决方案是什么?
我有以下宏:
#define F(Args, ...) \
// macro definition
#
F(()) // looks like usage?
#undef F
Run Code Online (Sandbox Code Playgroud)
该行只含有什么#
意思?是F(())
宏的用法吗?
你能解释为什么这段代码不能编译吗?
// source.cpp
constexpr const char* func(const char* s) { return s;}
constexpr bool find(const char *param) {
constexpr const char* result = func(param);
return (param == 0);
}
int main()
{
constexpr bool result = find("abcde");
}
Run Code Online (Sandbox Code Playgroud)
编译命令:
$ g++ -std=c++14 source.cpp
Run Code Online (Sandbox Code Playgroud)
我试过gcc5.4和gcc6.4.错误:
source.cpp: In function ‘constexpr bool find(const char*)’:
source.cpp:5:46: error: ‘param’ is not a constant expression
constexpr const char* result = func(param);
^
Run Code Online (Sandbox Code Playgroud) 我必须测试一个提供其自己的abort_routine()函数的库(在内部调用abort(),但是实现可能会改变)。
此abort_routine()的要求之一是它可能不会返回。
我想知道是否可以测试此要求?
更新: 我不使用gtest,只是llvm的亮起之类的东西:返回0,返回1,assert(false)。
正确吗
constexpr int* p = nullptr;
Run Code Online (Sandbox Code Playgroud)
声明constexpr指针(而不是constexpr int指针)?
这个定义
int* constexpr p = nullptr;
Run Code Online (Sandbox Code Playgroud)
给出编译错误。
引用C ++ 11:(18.2 / 9)
nullptr_t
定义如下:Run Code Online (Sandbox Code Playgroud)namespace std { typedef decltype(nullptr) nullptr_t; }
nullptr_t
作为同义词的类型具有在3.9.1和4.10中描述的特征。[注意:虽然nullptr
不能使用的地址,但是nullptr_t
可以使用作为左值的另一个对象的地址。—尾注]
我们是否需要类型的对象nullptr_t
(除外nullptr
)?
据我所知,它nullptr
是核心语言的一部分。
引用C ++ 11:(18.2 / 9)
nullptr_t
定义如下:
namespace std { typedef decltype(nullptr) nullptr_t; }
并且在标头中定义<cstddef>
。
以下是LLVM的libcxxabi中的函数:
void *__cxa_current_primary_exception() throw() {
// get the current exception
__cxa_eh_globals* globals = __cxa_get_globals_fast();
if (NULL == globals)
return NULL; // If there are no globals, there is no exception
__cxa_exception* exception_header = globals->caughtExceptions;
if (NULL == exception_header)
return NULL; // No current exception
if (!isOurExceptionClass(&exception_header->unwindHeader))
return NULL; // Can't capture a foreign exception (no way to refcount it)
if (isDependentException(&exception_header->unwindHeader)) {
__cxa_dependent_exception* dep_exception_header =
reinterpret_cast<__cxa_dependent_exception*>(exception_header);
exception_header = cxa_exception_from_thrown_object(dep_exception_header->primaryException);
}
void* thrown_object = thrown_object_from_cxa_exception(exception_header);
__cxa_increment_exception_refcount(thrown_object);
return thrown_object;
}
Run Code Online (Sandbox Code Playgroud)
全局变量是线程局部存储变量,因此thrown_object也特定于线程。我的理解是thrown_object是在线程中引发的异常-每个线程都可以引发自己的异常。
但是函数__cxa_increment_exception_refcount()自动执行增量操作-为什么?在什么情况下需要增量执行原子操作?
//test.cpp
#include <type_traits>
double* func() {}
static_assert(std::is_same<double*(*)(), decltype(func)>::value, "");
int main() {}
Run Code Online (Sandbox Code Playgroud)
编译命令:
g++ -std=c++11 -c test.cpp
Run Code Online (Sandbox Code Playgroud)
输出:
test4.cpp:6:1: error: static assertion failed:
static_assert(std::is_same<double*(*)(), decltype(func)>::value, "");
^
Run Code Online (Sandbox Code Playgroud)
上面的代码有什么问题?我该如何解决?
C ++样式指南强烈建议,如果我们不打算修改对象,则应将对象声明为const。因此,当我们声明一个不会被重新分配的对象/变量的指针时,应将其声明为const:
T* const pObject = new T();
Run Code Online (Sandbox Code Playgroud)
似乎C ++开发人员通常在使用指针的情况下不遵循此规则,对吗?
我需要过滤标题(摘要属性?)包含文本“ config
”的问题。所以我所做的是:
config
”但出于某种原因,我只得到了标题包含“ config
”或“ configs
”的问题。我没有得到标题包含例如“ configuration
”的问题。怎么了?
我通常会说“编译器警告”,“编译器错误”。因此,当听到“编译器诊断消息”或“编译器诊断”消息时,我不确定它们是否只是通常的“编译器警告”,“编译器错误”或其他内容?