Skip to content

关于模板 std::uniform_int_distribution 在类内声明强制要求 写明<>的问题 #249

Answered by Mq-b
MrShinshi asked this question in Q&A
Discussion options

You must be logged in to vote

你们这样写是不打算给后面的人快点看懂了吗?

template<typename T = int>
struct X{};

X x;    // Error! C++17 起 OK
X<> x2; // OK

必须达到 C++17CTAD,才可以在全局、函数作用域声明为 X 这种形式,省略 <>

但是如果在类中声明数据成员(静态或非静态,是否类内定义都无所谓),不管是否达到 C++17,都不能省略 <>

template<typename T = int>
struct X {};

struct Test{
    X x;                  // Error
    X<> x2;               // OK
    static inline X x3;   // Error
};

gcc13.2 有不同行为,开启 std=c++17,它可以通过编译,类内定义的静态数据成员省略 <>

template<typename T = int>
struct X {};

struct Test{
    X<> x2;               // OK
    static inline X x3;   // OK
};

int main(){
    
}

MinGw clang 16.02msvc 均不可通过编译。

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@MrShinshi
Comment options

Comment options

You must be logged in to vote
1 reply
@MrShinshi
Comment options

Answer selected by Mq-b
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants