我有一个 C++14 项目,无法使用 C++17 内联变量。
// myclass.h
class MyClass {
struct Inner {
using StringArray = std::array<const char*, 1>;
static constexpr StringArray kStrings{{ "foo" }};
}
}
//myclass.cpp
constexpr MyClass::Inner::StringArray kStrings;
// ^^^^^
// Error: "Inner" is a private member of "MyClass"
Run Code Online (Sandbox Code Playgroud)
是否可以让它在 C++14 中工作,或者只能在 C++17 中工作?
我有一个类,我想在编译时对其执行操作。在下面的示例中,我有一个IntWrapper带有重载的简单结构operator+。在任何正常上下文中使用此类似乎都可以正常工作,但在 lambda 内部使用时会失败,为什么?
struct IntWrapper
{
consteval IntWrapper operator+(const IntWrapper& other) const
{
return { x + other.x };
}
int x;
};
int main()
{
// Adding normally works just fine
constexpr IntWrapper c1 = IntWrapper{ 1 } + IntWrapper{ 1 };
auto adder = [](IntWrapper x) { return x + x; };
// Using a lambda to add doesn't work
constexpr IntWrapper c2 = adder(IntWrapper{ 1 });
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能让这个对象在编译时在 lambda 上下文中工作?
使用 constexpr 变量作为 case 标签是否正确?
#include <iostream>
int main() {
constexpr int x = 5;
int y = 4;
switch (y) {
case x - 1:
std::cout << "case " << x << std::endl;
break;
case 20:
std::cout << "case 20" << std::endl;
break;
default:
std::cout << "case default" << std::endl;
break;
}
}
Run Code Online (Sandbox Code Playgroud) C++ 11 iso标准对这样的表达式说了什么:
class MyClass
{
public:
constexpr int test()
{
return _x;
}
protected:
int _x;
};
Run Code Online (Sandbox Code Playgroud)
_x是a中使用的非常量constexpr:它会产生错误,还是会constexpr被忽略(就像我们传递非const参数时一样)?
以下代码无法与g ++ 4.8.2链接:
#include <map>
struct Foo
{
constexpr static int foo = 1;
};
static std::map<int, int> map {{1, Foo::foo}};
int main()
{
return Foo::foo;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
g++ -std=c++11 -o foo foo.cc
/tmp/ccZXCwiK.o: In function `__static_initialization_and_destruction_0(int, int)':
foo.cc:(.text+0x51): undefined reference to `Foo::foo'
Run Code Online (Sandbox Code Playgroud)
如果我评论出地图,事情就好了.这是一个编译器错误,还是我在标准中缺少的一些极端情况?
错误:
main.cpp: In function 'constexpr int fib(int)':
main.cpp:6:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
return (n < sizeof(ftbl)/sizeof(*ftbl)) ? ftbl[n] : fib(n-2) + fib(n-1);
^
/tmp/cch0aLwI.o: In function `main':
main.cpp:(.text.startup+0xf): undefined reference to `std::cout'
main.cpp:(.text.startup+0x16): undefined reference to `std::ostream::operator<<(int)'
main.cpp:(.text.startup+0x2d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)'
main.cpp:(.text.startup+0x3c): undefined reference to `std::cout'
main.cpp:(.text.startup+0x43): undefined reference to `std::ostream::operator<<(int)'
main.cpp:(.text.startup+0x5a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char …Run Code Online (Sandbox Code Playgroud) 由于有大量的提案,我想问一下,如果C++中存在这样的事情,或者是否有任何提议做这样的事情.
理念:
template <typename T>
constexpr typename evalToType(int x, int y) {
if(x > y)
return T;
else
return int;
}
template <typename T, int x, int y>
evalToType<T>(x, y) SomeFunction() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这样做的动机是微不足道和简单的,基本上模板可能非常复杂,并且它变得非常难以理解,所以为什么不将模板SFINAE表示为constexpr,它基本上返回一个类型而不是一个值.
我想用结构名称的哈希值初始化结构成员。
constexpr uint32_t myHash(const char* const data)
{ //Some code for hash
return myHash;
}
struct My_Struct{
constexpr Test() : ID(myHash("My_Struct"))
{
}
const uint32_t ID;
}
Run Code Online (Sandbox Code Playgroud)
当我有:
constexpr My_Struct my_constexpr_struct;
Run Code Online (Sandbox Code Playgroud)
然后,在编译时成功计算了哈希。但是,当我有主要职能时
My_Struct my_normal_struct;
Run Code Online (Sandbox Code Playgroud)
然后它将调用
constexpr uint32_t myHash(const char* const data)
Run Code Online (Sandbox Code Playgroud)
而不是简单地使用编译时间常数初始化struct成员。
显然,这将导致可避免的重大性能损失。
关于如何在编译时执行编译器的任何想法或建议?我真的不想做:
constexpr uint32_t MY_STRUCT_ID = myHash("My_Struct");
struct My_Struct{
constexpr Test() : ID(MY_STRUCT_ID)
{
}
const uint32_t ID;
Run Code Online (Sandbox Code Playgroud)
谢谢。
我constexpr在C++ 17中使用此参考链接阅读了该文章.
然后,我制作了C++程序进行测试constexpr:
#include <iostream>
int i = 10;
int func()
{
if constexpr (i == 0)
return 0;
else if (i > 0)
return i;
else
return -1;
}
int main()
{
int ret = func();
std::cout<<"Ret : "<<ret<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器会出错:
main.cpp: In function 'int func()':
main.cpp:8:25: error: the value of 'i' is not usable in a constant expression
if constexpr (i == 0)
^
main.cpp:4:5: note: 'int i' is not const
int …Run Code Online (Sandbox Code Playgroud) 我确实理解constexpr在运行时可以计算的表达式上使用它时的用法.
我想为复数创建一个constexpr.x = 5_i应该创建一个复杂数量的我自己创建的复杂类,并且我需要一个constantexpr constructor.
class Complex {
private:
double real_;
double imag_;
public:
...
Complex(double real, double imaginary);
constexpr Complex(double real, double imaginary):
real_(real),imag_(imaginary) {};
//Nonmember function
constexpr Complex operator""_i(long double arg);
Run Code Online (Sandbox Code Playgroud)
在Complex(double real, double imaginary);稍后在.cpp文件中定义的.
当我尝试编译它时,我收到以下错误:
‘constexpr Complex::Complex(double, double)’ cannot be overloaded with
‘Complex::Complex(double, double)’
Run Code Online (Sandbox Code Playgroud)
如果我只定义constexpr函数我的结论是我不能Complex::Complex(double, double)在运行时使用.
为什么我不能定义两个不同的功能?这在C++中是不允许的?编译器能否看到两个函数之间的区别?还有其他办法吗?