这是一段代码:
class Class
{
static constexpr int getBug();
};
constexpr int Class::getBug()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我基本上做的是static constepxr在类声明中声明一个方法,然后我实现它.
原始代码分为两个文件,并包含更多已剥离的方法/属性,只留下所需的代码.
当我编译代码时,我从GCC 4.6.0得到以下错误:
Class.cpp:6:29: internal compiler error: in merge_types, at cp/typeck.c:825
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
Run Code Online (Sandbox Code Playgroud)
这真的是个bug吗?
在那种情况下,我必须提供什么报告?
我已经在在线C++ 0x编译器上测试了代码并得到以下错误:
prog.cpp:3:33: error: the 'constexpr' specifier cannot be used in a function declaration that is not a definition
prog.cpp:6:29: error: a constexpr function cannot be defined outside of its …Run Code Online (Sandbox Code Playgroud) 我试图运行一个基于的程序constexpr.
码:-
#include <iostream>
using namespace std;
int main()
{
const int i = 10;
constexpr int j = 10;
constexpr int val1 = i;
constexpr int val2 = j;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在我所遵循的书中,提到如果将const分配给constexpr变量,那就是错误.
但我的程序编译没有任何投诉.
我错过了什么吗?
Visual Studio 2015更新3。
我读了《编程》。Bjarne Stroustrup的《使用C ++的原理和实践》(第二版)。我学习constexpr功能...
有用:
constexpr int get_value(int n) {
return n + 1;
}
Run Code Online (Sandbox Code Playgroud)
但是我不能编译它(代替第一个变体):
constexpr int get_value(int n) {
return ++n;
}
Run Code Online (Sandbox Code Playgroud)
我得到错误:
constexpr函数返回的值不是恒定的
该n是本地的变量get_value函数。即n变量更改不会影响外部代码。
为什么该get_value函数的第二个变体是错误的?
可以将ASCII文本(不能将其称为字符串)分配给enum值,如下所示:
#include <iostream>
// Macro to handle BIG/LITTLE ENDIAN
// Endianness is suppoesed to handled in this macro
#define TEMP(X) X
enum t
{
XX = 'AA', // 0x4141 or 0100 0001 0100 0001
};
int main()
{
std::cout<<XX<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
0x4141在这种情况下,编译器会编译它并在编译时生成一个Hexa-decimal常量.它确实生成一个编译警告:
main.cpp:9:14: warning: multi-character character constant [-Wmultichar]
XX = 'AA', // 0x4141 or 0100 0001 0100 0001
Run Code Online (Sandbox Code Playgroud)
我的问题是,我们可以避免这种警告吗?
或者我们可以编写一个更优雅的代码来实现类似的结果,可能使用模板和constexpr?
我正在寻找一种便携式替代方案,这样我就可以在不影响核心逻辑的情况下将其作为重构的一部分.
考虑到一个程序完全由constexpr函数组成(它们都能在编译时计算),对于"level"深度调用constexpr函数的方式是否存在限制?通过仅使用constexpr函数,在给定正确的必要条件的情况下,是否可以在编译时计算整个程序?
请考虑以下代码:
template <class /* Weird Hack Here */>
struct object
{
constexpr object(int value, /* Other Weird Hack */) noexcept;
};
int main()
{
object foo(1);
object bar(1);
}
Run Code Online (Sandbox Code Playgroud)
是否有可能在C++ 17中使用奇怪的技巧来拥有foo和bar成为不同的类型?
请参阅以下代码:
#include <iostream>
constexpr int f(int a, int b)
{
return a<b? a : throw std::out_of_range("out of range");
}
int main()
{
try
{
int n = 0;
f(5, n);
}
catch(const std::exception& ex)
{
std::cout<<"Exception caught"<<std::endl;
std::cout<<ex.what()<<std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道constexprt函数在编译时处理.那么我怎么能将"运行时"本地变量传递给它并在运行时再次在try-catch块中使用它?也许我错过了smth regargind constexprt的功能?
我已经实现了一个constexpr编译时哈希函数,如果被调用,它可以正常工作(即在编译时进行评估)
constexpr auto hash = CompileTimeHash( "aha" );
Run Code Online (Sandbox Code Playgroud)
但是我需要在实际代码中使用它作为函数的参数
foo( CompileTimeHash( "aha" ) ); // foo is NOT constexpr
Run Code Online (Sandbox Code Playgroud)
由于某个特定原因,我不能使用长版本
constexpr auto hash = CompileTimeHash( "aha" );
foo( hash );
Run Code Online (Sandbox Code Playgroud)
编译器(VC++)在短(第一)情况下不会编译时哈希.有没有办法实现这个目标?
编辑:现在可以找到一个覆盖3个案例的例子:https: //godbolt.org/z/JGAyuE 只有gcc在所有3个案例中完成它
来自C ++ Now 2017的Jason Turner和Ben Deane有一个很好的演讲,constexpr All the things它也给出了constexpr向量实现。出于教育目的,我本人也想这个主意。我的constexpr向量是纯粹的,因为将其推回会返回带有添加元素的新向量。
在演讲中,我看到了以下push_back实现方案:
constexpr void push_back(T const& e) {
if(size_ >= Size)
throw std::range_error("can't use more than Size");
else {
storage_[size_++] = e;
}
}
Run Code Online (Sandbox Code Playgroud)
他们通过价值来考虑要素并加以移动,但是,我认为这不是我的问题的根源。我想知道的是,该函数如何在constexpr上下文中使用?这不是const成员函数,它修改状态。我不认为可以做类似的事情
constexpr cv::vector<int> v1;
v1.push_back(42);
Run Code Online (Sandbox Code Playgroud)
如果这不可能,那么我们如何在constexpr上下文中使用此东西,并使用此向量(即编译时JSON解析)实现任务的目标?
这是我的版本,以便您可以同时看到我的新引导程序返回版本和谈话中的版本。(请注意,忽略了性能,完美转发等问题)
#include <cstdint>
#include <array>
#include <type_traits>
namespace cx {
template <typename T, std::size_t Size = 10>
struct vector {
using iterator = typename std::array<T, Size>::iterator;
using const_iterator = typename std::array<T, Size>::const_iterator;
constexpr vector(std::initializer_list<T> const& l) …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现基于特征的策略子系统,但是我有一个我真的不知道该如何解决的问题(如果可能的话)。我的特征看起来像这样:
template <typename ValueT, typename TagT = void, typename EnableT = void>
struct TPolicyTraits
{
static void Apply(ValueT& value) { }
};
Run Code Online (Sandbox Code Playgroud)
这个特征可以这样专门化:
struct MyPolicy {};
template <typename ValueT>
struct TPolicyTraits<ValueT, MyPolicy>
{
static void Apply(ValueT& value) { /* Implementation */ }
};
Run Code Online (Sandbox Code Playgroud)
我想在编译时以某种链表的形式注册策略。策略系统将像这样使用:
namespace PolicyTraits
{
template <typename ValueT, typename TagT>
using TPolicyTraitsOf = TPolicyTraits<std::decay_t<ValueT>, TagT>;
template <typename ValueT>
void Apply(ValueT&& value)
{
// todo iterate through constexpr tag list and apply policies
}
template <typename TagT>
constexpr void …Run Code Online (Sandbox Code Playgroud)