小编Rom*_*lka的帖子

模板编译应该会失败,但只要不使用有问题的函数,编译就会成功

我希望下面的代码无法编译,因为asf的成员函数没有提供所需的成员。实际上,即使关闭优化,它在不使用时也可以很好地编译。这怎么可能?Exclamator<B>Bnamef

#include <string>
#include <iostream>

class A {
public:
    std::string name() { return "A"; }
};

class B {} ;

template <typename T>
class Exclamator {
public:
    Exclamator(T a): x{a} {}

    void f() {
        std::cout << x.name() << std::endl;
    }
private:
    T x;
};

int main() {
    A a;
    Exclamator xa {a};
    xa.f();

    B b;
    Exclamator xb {b};
//    xb.f();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ g++ -std=c++17 -O0 main.cpp
$ ./a.out
A
Run Code Online (Sandbox Code Playgroud)

c++ templates

3
推荐指数
1
解决办法
52
查看次数

bash 脚本中的这些范围更改在做什么?

如果你有这样的事情:

#!/bin/bash
(
  x=foo
  { echo $x }
)
Run Code Online (Sandbox Code Playgroud)

括号和大括号的意义是什么?这些结构称为什么?他们有什么属性?它们用于什么等?

bash

2
推荐指数
1
解决办法
32
查看次数

标签 统计

bash ×1

c++ ×1

templates ×1