簡體   English   中英

將 const T& 作為 this 傳遞給 constexpr 成員函數

[英]Passing const T& as this to a constexpr member function

我正在嘗試修復一些庫代碼,其中簡化的最小版本如下所示:

 #include <iostream>

 template <typename RangeT>                                          
 struct formatter {                               
     constexpr void format(const RangeT& values) {    
         for (auto it = values.begin(), end = values.end(); it != end; ++it) { 
             std::cout << it << "\n";                
         }                                                                       
     }                                    
 }; 

template <typename RangeT, typename Formatter> 
struct type_erased {                                                       
    static void format(const void* arg) {                                
        Formatter f;                   
        f.format(*static_cast<const RangeT*>(arg)); 
    }                                                  
};                                                                 

struct view {   
    int count_;                                

    constexpr View(int count) : count_(count) {}  

    constexpr int 
    begin() { return 0; }                   

    constexpr int                                                    
    end() { return -1; } 
};                      

int                                                         
main()                                                         
{                               
    View view(5);       
    void* ptr = static_cast<void*>(&view);                          
    type_erased<View, formatter<View>>::format(ptr); 
}                                                                       

上面的代碼不能在 GCC 中編譯,因為:

../src/view.cpp: In instantiation of ‘constexpr void formatter<RangeT>::format(const RangeT&) [with RangeT = View]’:
../src/view.cpp:21:9:   required from ‘static void type_erased<RangeT, Formatter>::format(const void*) [with RangeT = View; Formatter = formatter<View>]’
../src/view.cpp:43:41:   required from here
../src/view.cpp:11:15: error: passing ‘const View’ as ‘this’ argument discards qualifiers [-fpermissive]
   11 |     for (auto it = values.begin(), end = values.end(); it != end; ++it) {
  |               ^~
../src/view.cpp:31:5: note:   in call to ‘constexpr int View::begin()’
   31 |     begin() { return 0; }
  |     ^~~~~
../src/view.cpp:11:36: error: passing ‘const View’ as ‘this’ argument discards qualifiers [-fpermissive]
   11 |     for (auto it = values.begin(), end = values.end(); it != end; ++it) {
  |                                    ^~~
../src/view.cpp:34:5: note:   in call to ‘constexpr int View::end()’
   34 |     end() { return -1; }

constexpr 成員函數中關於this的規則是什么? 它是否受為函數參數指定的規則或有特殊限制?

我將如何解決此錯誤? 如果它只是格式化程序結構,我將使用RangeT&&std::move因為據我所知,視圖根據定義在 O(1) 中是可復制的。 我不知道如何使用混合中的類型擦除步驟來做到這一點......

提前致謝,理查德

我認為這與constexpr沒有任何關系。

您有對const RangeT的引用,並且您正試圖在其上調用非const成員函數( begin()end() )。

如果您想允許,請提供const重載(和/或cbegin() / cbegin() cend()變體)。

在您的代碼中,由於 begin 和 end 不是 const 函數,因此this指針不能指向const對象而不“丟棄限定符”。

通過使函數為constthis指針可以指向一個const對象。

https://en.cppreference.com/w/cpp/language/member_functions#const-.2C_volatile-.2C_and_ref-qualified_member_functions

https://en.cppreference.com/w/cpp/language/this

https://godbolt.org/z/3_wKh9

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM