constexpr and its difference between define:

constexpr and its difference between define:

Def: constexpr is a keyword that specifies that an expression can be evaluated at compile time

Features:

    1. the function will be evaluated at compile time
    1. must has a single return statement and no side effects
    1. must be executed with constant arguments
    constexpr int factorial(int n){
        return (n <= 1) ? 1 : n * factorial(n-1);
    }

    constexpr int x = factorial(5); // x is evaluated at compile time to be 120

Note that constexpr functions can also be used in non-constant expressions, in which case they will be evaluated at runtime just like regular functions.

Here is an example of constexpr in both constant and non-constant case:

  • #include <iostream>
    
    constexpr int factorial(int n) {
        return n <= 1 ? 1 : n * factorial(n - 1);
    }
    
    int main() {
        // Constant expression
        constexpr int x = factorial(5); // x = 120 will be determined in compile time
        std::cout << "Factorial of 5 is " << x << std::endl; // 120
    
        // Non-constant expression
        int y = 6;
        int z = factorial(y); // z is determind at run time, like a regular func
        std::cout << "Factorial of " << y << " is " << z << std::endl; // 720
    
        return 0;
    }
    
    

Difference:

Both of them can define constant. However, constexpr is a compile-time constatns, meaning that their values must be determinable at compile time. Macro definition is a simple text replacements without type information and cannot be checked at compile time. Using macro definitions to define constants can therefore lead to type errors or incorrect values that may only be discovered at runtime.

In a nutshell, constexpr constants are typically more type-safe and can provide better compile-time checks, while macro definitions can have type issues and may be difficult to catch at compile time.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是因为编译器无法确定 `std::hash<std::string>::operator()` 函数的返回值是否是编译期常量,因此不能在 `constexpr` 上下文中使用该函数。 解决方法是使用 `constexpr` 关键字显式地声明 `std::hash<std::string>::operator()` 函数是一个 `constexpr` 函数。示例代码如下: ```c++ #include <iostream> #include <functional> #include <string> using namespace std; // 显式声明 std::hash<std::string>::operator() 是一个 constexpr 函数 namespace std { template<> struct hash<string> { constexpr size_t operator()(const string& str) const { return std::_Hash_impl::hash(str.data(), str.length()); } }; } int main() { unordered_map<size_t, string> map = { {hash<string>()("hello"), "Hello World!"}, {hash<string>()("world"), "World Hello!"}, {hash<string>()("good"), "Good Morning!"}, {hash<string>()("night"), "Good Night!"} }; string str; cout << "Please enter a string: "; getline(cin, str); switch (hash<string>()(str)) { case hash<string>()("hello"): cout << map[hash<string>()("hello")] << endl; break; case hash<string>()("world"): cout << map[hash<string>()("world")] << endl; break; case hash<string>()("good"): cout << map[hash<string>()("good")] << endl; break; case hash<string>()("night"): cout << map[hash<string>()("night")] << endl; break; default: cout << "Unknown input!" << endl; break; } return 0; } ``` 在上面的代码中,我们显式声明了 `std::hash<std::string>::operator()` 函数是一个 `constexpr` 函数,编译器就可以确定该函数的返回值是编译期常量,从而避免了上述编译错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值