我在接口中有一个方法,我想用便携式C++弃用.当我用Google搜索时,我得到的只是Microsoft特定的解决方案; #pragma deprecated和__declspec(不建议使用).
二等奖解决方案是ifdef MSVC和GCC解决方案.
谢谢
在react-router v5中我创建了这样的历史对象:
import { createBrowserHistory } from "history";
export const history = createBrowserHistory();
Run Code Online (Sandbox Code Playgroud)
然后将其传递给路由器:
import { Router, Switch, Route, Link } from "react-router-dom";
<Router history={history}>
... my routes
</Router>
Run Code Online (Sandbox Code Playgroud)
我这样做是为了有机会在组件之外使用历史记录:
// store action
logout() {
this.user = null;
history.push('/');
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我将逻辑移至商店,并且组件尽可能保持干净。但现在,在 React Router v6 中我不能做同样的事情。我仍然可以useNavigate()在我的组件内部使用它进行导航,但我无法创建一个navigate将其使用到我的商店中的组件。还有其他选择吗?
我想知道在类中声明模板功能是否有任何优势.
我试图清楚地了解这两种语法的优缺点.
这是一个例子:
不合时宜:
template<typename T>
struct MyType {
template<typename... Args>
void test(Args...) const;
};
template<typename T>
template<typename... Args>
void MyType<T>::test(Args... args) const {
// do things
}
Run Code Online (Sandbox Code Playgroud)
同班同学:
template<typename T>
struct MyType {
template<typename... Args>
void test(Args... args) const {
// do things
}
};
Run Code Online (Sandbox Code Playgroud)
是否有第一版或第二版更容易使用的语言功能?使用默认模板参数或enable_if时,第一个版本是否会妨碍?我想看看这两个案例如何使用不同的语言特征(如sfinae)以及未来潜在的特征(模块?)进行比较.
将编译器特定行为考虑在内也很有趣.我认为MSVC需要inline在某些地方使用第一个代码片段,但我不确定.
编辑:我知道这些功能的工作方式没有区别,这主要是品味问题.我想看看两种语法如何使用不同的技术,以及一种优于另一种的优势.我看到大多数答案都有利于一个人,但我真的很想得到双方的支持.更客观的答案会更好.
默认情况下,C++ 17中的模板是否内联静态变量?这是一个例子:
template<typename T>
struct SomeClass {
static T test;
};
struct SomeClass2 {
static constexpr int test = 9;
};
Run Code Online (Sandbox Code Playgroud)
这些变量是内联的还是仍然需要使用ODR来定义ODR?
我从JSON对象得到一个负数.我想删除负号的" - "形式,只显示绝对值.
收到json:
{
"value": -2.34
}
Run Code Online (Sandbox Code Playgroud)
我要展示的内容:
值为:2.34
怎么find_type知道功能typemap在哪里?
它收到的参数不是来自该命名空间,而是来自std命名空间!
#include <type_traits>
#include <memory>
namespace lib {
template<typename T>
struct find_type {
using type = decltype(typemap(std::declval<T>()));
};
}
namespace test {
struct Test {};
auto typemap(std::unique_ptr<Test>) -> int;
}
static_assert(std::is_same<int, lib::find_type<std::unique_ptr<test::Test>>::type>::value, "");
Run Code Online (Sandbox Code Playgroud)
这段代码怎么样?允许这个的规则是什么?
我用GCC 6.3和clang 3.9.1测试了它.
我正在阅读有关模块的内容,我希望这样做:
a.cpp
module foo.a;
export namespace foo {
struct A {
void doA();
};
}
import foo.b;
void foo::A::doA() {
B{}.doB();
}
Run Code Online (Sandbox Code Playgroud)
b.cpp
module foo.b;
export namespace foo {
struct B {
void doB();
void start();
};
}
import foo.a;
import std.io;
void foo::B::doB() {
std::cout << "Stuff done!" << std::endl;
}
void foo::B::start() {
A{}.doA();
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
import foo.b;
int main() {
foo::B{}.start();
}
Run Code Online (Sandbox Code Playgroud)
由于模块接口不能互相使用,为了使其工作,导出的命名空间之后的所有内容都不能成为接口的一部分.根据目前的TS,上述是否正确?对于实现中的循环依赖,是否需要将其拆分为另一个文件?
给定以下类模板:
template<typename T>
struct Outer
{
struct Inner;
auto f(Inner) -> void;
};
Run Code Online (Sandbox Code Playgroud)
我们Inner为以下每种专业分别定义Outer:
template<>
struct Outer<int>::Inner {};
template<>
struct Outer<double>::Inner {};
Run Code Online (Sandbox Code Playgroud)
然后f为以下所有专业定义一次成员函数Outer:
auto Outer<T>::f(Inner) -> void
{
}
Run Code Online (Sandbox Code Playgroud)
但是Clang(9.0.0)抱怨:
error: variable has incomplete type 'Outer::Inner'
auto Outer<T>::f(Inner) -> void
^
Run Code Online (Sandbox Code Playgroud)
我们还可以通过提供Inner以下所有其他专业的定义来规避编译器错误Outer:
template<typename T>
struct Outer<T>::Inner {};
Run Code Online (Sandbox Code Playgroud)
或通过f为每个专业分别定义:
template<>
auto Outer<int>::f(Inner) -> void
{
}
template<>
auto Outer<double>::f(Inner) -> void
{
}
Run Code Online (Sandbox Code Playgroud)
GCC和MSVC都接受初始代码,这就是问题所在。这是Clang错误,还是这三个中唯一的一致实现?
c++ templates template-specialization language-lawyer incomplete-type
我今天尝试做类似的事.我很惊讶它没有编译.
struct Test {
// v----- Remove me to compile
// /*
static constexpr auto get_test1 = [](Test const& self) {
return self.test; // error, Test is incomplete
};
// */
// Handwritten version of the lambda
struct {
constexpr auto operator() (Test const& self) const {
return self.test; // ok
}
}
static constexpr get_test2{};
int test;
};
Run Code Online (Sandbox Code Playgroud)
它说Test范围内的类型不完整.然而,lambda的手写版确实有效.这是什么技术原因?这是标准中的疏忽,还是有一个特定的措辞Test在lambda中不完整?
c++ ×8
c++17 ×3
templates ×3
angularjs ×1
c++-modules ×1
c++11 ×1
clang++ ×1
inline ×1
javascript ×1
lambda ×1
react-router ×1
reactjs ×1