我想unique_ptr用一个特殊的析构函数重新定义.因此,我使用下面的代码,我试图模仿一些构造函数unique_ptr.不幸的是,constexpr构造者拒绝构建,我不知道为什么.
class job_ptr : public unique_ptr<Job>
{
public:
constexpr job_ptr()
: unique_ptr<Job>(), sequencer( nullptr ) {}
constexpr job_ptr( nullptr_t )
: unique_ptr<Job>( nullptr ), sequencer( nullptr ) {}
private:
FIFOSequencer* sequencer;
};
Run Code Online (Sandbox Code Playgroud)
初始化列表中的两个构造函数都被声明constexpr,但是clang++考虑constexpr constructor never produces a constant expression因为non-literal type 'unique_ptr<Job>' cannot be used in a constant expression.这意味着什么? constexpr构造函数不能在constexpr构造函数中使用?
感谢您的任何帮助.
我正在尝试了解在测试更改时我所看到的内容.该平台是带有GCC 4.8的openSUSE 42,但它可能会影响其他人.测试代码和错误如下.
$ cat test.cxx
#include <string>
#if (__cplusplus >= 201103L)
# define STATIC_CONSTEXPR static constexpr
# define CONSTEXPR constexpr
#else
# define STATIC_CONSTEXPR static const
# define CONSTEXPR
#endif
struct Name
{
STATIC_CONSTEXPR char* GetName() {return "XXX";}
};
int main(int argc, char* arv[])
{
const char* name = Name::GetName();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和:
$ g++ -O3 -std=c++11 test.cxx -o test.exe
test.cxx: In static member function ‘static constexpr char* Name::GetName()’:
test.cxx:13:44: warning: deprecated conversion from string constant to ‘char*’ …Run Code Online (Sandbox Code Playgroud) 我正在学习,constexpr并且据我所知,它constexpr告诉编译器在编译期间计算函数而不是运行时间.我使用以下代码进行测试,但遇到了一个我真的不明白的错误.你能解释一下原因吗?
#include <iostream>
#include <array>
using namespace std;
constexpr int foo(int i)
{
return i + 5;
}
int main()
{
int i = 10;
std::array<int, foo(5)> arr; // OK
// But...
std::array<int, foo(i)> arr1; // Error
}
Run Code Online (Sandbox Code Playgroud)
错误是:' i' 的值在常量表达式中不可用.为什么?i事先声明为什么它必须是一个const?
我试图在异常消息中使用constexpr,但这不起作用:后续代码在g ++上编译得很好(使用c ++ 11或c ++ 14).
#include <exception>
constexpr auto TEST = "test";
class test_throw : public std::exception {
public:
virtual const char* what() const throw() {
return (std::string("THROW ")+TEST).c_str();
}
};
int main()
{
throw test_throw{};
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么我的异常输出一个空消息,好吧这似乎是一个坏技巧,但我不明白消息是如何为空.
有没有办法实现这一点而不用宏替换constexpr?
任何人都可以帮我理解为什么这段代码片段无法编译?
#include <iostream>
#include <tuple>
#include <string_view>
constexpr auto Fields()
{
using namespace std::string_view_literals;
return std::tuple(
std::tuple("campo_1"sv, 123),
std::tuple("campo_2"sv, 456),
std::tuple("campo_3"sv, 890),
std::tuple("campo_4"sv, 136));
}
template<typename Tuple>
constexpr auto ProcessTupleElement(Tuple &&tuple)
{
//constexpr auto ccb = std::get<1>(tuple); // 1 uncomment to get an error
//char sx[ccb]; // 1 uncomment to get an error
//(void)sx;
return std::get<1>(tuple);
}
template<typename Tuple>
constexpr auto IterateTupleImpl(Tuple &&t)
{
return ProcessTupleElement(std::get<0>(std::forward<Tuple>(t)));
}
template<typename Tuple>
constexpr auto IterateTuple(Tuple &&t)
{
return IterateTupleImpl(std::forward<Tuple>(t));
}
int main()
{ …Run Code Online (Sandbox Code Playgroud) 出于某种原因,我很难掌握如何正确使用constexpr.
标题中描述的情况是否适合使用它?即:
void foo()
{
static constexpr const size_t MAX_BUFFER_SIZE = 20 * 1024 * 1024;
constexpr size_t bufferSize = 1024 * 1024; // Initialized with constant expression
std::vector<char> buffer(bufferSize, ' ');
//...
if (some_condition())
{
bufferSize = get_random_value_at_runtime(); // Assigned a new 'non-constexpr' value
buffer.resize(bufferSize, ' ');
}
//...
}
Run Code Online (Sandbox Code Playgroud)
亲切的问候!
我编写了以下C++ 17代码:
constexpr bool gDebug = true;
template <typename T> constexpr const T& Select(const bool pCondition, const T& a, const T& b)
{
if constexpr (pCondition)
{
return a;
}
else
{
return b;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我这样打电话:
int c = Select<QString>(gDebug, a, b); // In .cpp
Run Code Online (Sandbox Code Playgroud)
我找到error: ‘pCondition’ is not a constant expression了这if constexpr条线.
为什么?这不应该工作吗?
以下按预期工作:
#include <array>
constexpr std::array<int, 3> values = {1, 2, 3};
template <int i> struct A { static constexpr int val = values[i]; };
int main() { A<1> a; }
Run Code Online (Sandbox Code Playgroud)
但是,如果我们使用values.size()模板参数,我会从MSVC编译器中收到编译器错误:
int main() { A<values.size()> a; }
Run Code Online (Sandbox Code Playgroud)
错误是表达式没有评估为常量.GCC编译没有错误.
我有以下程序:
#include <iostream>
void Init();
struct Foo {
Foo() {
int *p = new int; // just to make sure Foo's ctor is not a constant expression
Init();
}
} foo;
struct Bar {
constexpr Bar()
: value(0) { }
int value;
} bar;
void Init() {
bar.value = 1;
}
int main()
{
std::cout << bar.value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这里foo的构造函数不是常量表达式,因此我们将动态初始化foo。但是bar的构造函数似乎是一个常量表达式,因此我们将对进行静态初始化bar。因此,bar必须先调用的ctor,然后foo将其1视为输出。我在GCC 8.3.0和Clang 8.0.0中观察到了这样的结果。但是对于Visual C ++,实际的输出是,0 …
c++ static-variables visual-c++ static-initialization constexpr
我试图探究某个功能的含义,inline并偶然发现了这个问题。考虑这个小程序(demo):
/* ---------- main.cpp ---------- */
void other();
constexpr int get()
{
return 3;
}
int main()
{
std::cout << get() << std::endl;
other();
}
/* ---------- other.cpp ---------- */
constexpr int get()
{
return 4;
}
void other()
{
std::cout << get() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
在不进行优化的情况下进行编译时,该程序将产生以下输出:
3
3
Run Code Online (Sandbox Code Playgroud)
可能不是我们想要的,但是至少我可以解释一下。
constexpr在编译时计算函数结果,因此决定将其推迟到运行时。constexpr 在功能上意味着 inlineget()功能碰巧有不同的实现get()函数声明为静态get()功能的一种实现碰巧的是,链接器get()从中选择main.cpp,返回了3。
现在到我不了解的部分。我只是将get()功能从更改constexpr为 …
c++ ×10
constexpr ×10
c++11 ×5
c++14 ×1
c++17 ×1
c++20 ×1
consteval ×1
constructor ×1
exception ×1
if-constexpr ×1
stdarray ×1
templates ×1
unique-ptr ×1
visual-c++ ×1