5

c++ 模版编译问题, func -> bool { if constexpr A else B } 和直接声明 static co...

 2 years ago
source link: https://www.v2ex.com/t/815172
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  C++

c++ 模版编译问题, func -> bool { if constexpr A else B } 和直接声明 static constexpr bool 编译的区别

  sl0000 · 17 小时 22 分钟前 · 525 次点击
struct is {
    template<typename T>
    static constexpr bool _vector() {
        if constexpr (std::is_fundamental_v<T>) {
            return false;
        } else {
            return std::is_same_v<T, std::vector<typename T::value_type>>;
        }
    }
    template<typename T>
    static constexpr bool vector = _vector<T>();
    
    template<typename T>
    static constexpr bool vector2 = std::is_fundamental_v<T> || (!std::is_fundamental_v<T> && std::is_same_v<T, std::vector<typename T::value_type>>);
};



int main(int argc, const char * argv[]) {
    std::cout << is::vector2<std::vector<std::vector<int>>::value_type::value_type> << std::endl;
    return 0;
}

使用 vector 是 ok 的

使用 vector2 编译会报 Type 'int' cannot be used prior to '::' because it has no members


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK