我有许多不相关的类型,它们都通过重载的自由函数(ad hoc多态)支持相同的操作:
struct A {};
void use(int x) { std::cout << "int = " << x << std::endl; }
void use(const std::string& x) { std::cout << "string = " << x << std::endl; }
void use(const A&) { std::cout << "class A" << std::endl; }
Run Code Online (Sandbox Code Playgroud)
正如问题的标题所暗示的那样,我希望将这些类型的实例存储在异构容器中,这样use()
无论它们具体是什么类型,我都可以使用它们.容器必须具有值语义(即两个容器之间的赋值复制数据,它不共享它).
std::vector<???> items;
items.emplace_back(3);
items.emplace_back(std::string{ "hello" });
items.emplace_back(A{});
for (const auto& item: items)
use(item);
// or better yet
use(items);
Run Code Online (Sandbox Code Playgroud)
当然,这必须是完全可扩展的.考虑一个带有a的库API vector<???>
,以及将自己的类型添加到已知类型的客户端代码.
通常的解决方案是将(智能)指针存储到(抽象)接口(例如vector<unique_ptr<IUsable>>
),但这有许多缺点 - 从我的头脑:
作为我上一个问题的后续,我试图检测是否存在需要显式特化的模板函数.
我当前的工作代码检测非模板函数(感谢DyP的帮助),前提是它们至少使用一个参数,以便可以使用从属名称查找:
// switch to 0 to test the other case
#define ENABLE_FOO_BAR 1
namespace foo {
#if ENABLE_FOO_BAR
int bar(int);
#endif
}
namespace feature_test {
namespace detail {
using namespace foo;
template<typename T> decltype(bar(std::declval<T>())) test(int);
template<typename> void test(...);
}
static constexpr bool has_foo_bar = std::is_same<decltype(detail::test<int>(0)), int>::value;
static_assert(has_foo_bar == ENABLE_FOO_BAR, "something went wrong");
}
Run Code Online (Sandbox Code Playgroud)
(ENABLE_FOO_BAR
宏仅用于测试目的,在我的实际代码中我没有这样的宏可用,否则我不会使用SFINAE)
当模板函数的模板参数可以由编译器自动推导时,这也适用于模板函数:
namespace foo {
#if ENABLE_FOO_BAR
template<typename T> int bar(T);
#endif
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试检测需要显式特化的模板函数时,存在时才会static_assert
启动:foo::bar()
namespace …
Run Code Online (Sandbox Code Playgroud) 我正在使用LESS CSS 1.3.3.对不起,如果已经提出这个问题,我在网上找不到任何相关内容.
我有几个看起来像这样的类生成器(示例非常简化,足以触发错误):
#genMarginTop (@name, @size) {
.@{name} { margin-top: @size; }
}
Run Code Online (Sandbox Code Playgroud)
然后我用它们来生成一些实际的类:
#genMarginTop(mtStandard, 40px);
#genMarginTop(mtHalf, 20px);
Run Code Online (Sandbox Code Playgroud)
到目前为止,很好,LESS正确生成了这些类,我可以在HTML中使用它们.但是,当我想在其他地方重用这样生成的类作为mixin时,我收到一个错误:
.someClass {
.mtStandard; // won't work, see error below
// more stuff
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
NameError: .mtStandard is undefined in /.../example.less:161:4
160 .someClass {
161 .mtStandard;
162 // more stuff
Run Code Online (Sandbox Code Playgroud)
当然,我尝试在生成类之后使用mixin .它看起来像 LESS以某种方式在生成它们之后不会在内部注册这些生成的类,但我可能错了.
有没有办法在其他类中重用这些生成的类作为mixins?作为LESS的新手,他们的文档对生成的类相当稀疏,我完全失去了(特别是因为这是mixins似乎接受的唯一语法).
谢谢你读我
注意:我使用这样的类生成器的原因是因为它们比上面的例子复杂得多(想想所有依赖于一组公共参数的嵌套类),并且我将生成的类嵌入到各种@media
查询中以支持任何设备类型采用"Zen"方式.最后我得到了类似的东西:
@media (max-width: 1024px) {
#genSomething(something, somethingParam1, ...);
#genSomething(somethingElse, somethingElseParam1, ...);
#genStuff(stuff, stuffParam1, ...);
}
@media (max-width: 240px) { …
Run Code Online (Sandbox Code Playgroud) 在最近的讨论中,问题是我们是否应该始终完全限定类定义中当前类的名称,并且在引用当前模板本身时也使用显式专用模板.把它们加起来:
namespace foo {
struct Foo {
doSomething(foo::Foo& other); // arguably good
doSomething(Foo& other); // arguably bad
};
}
template<typename T>
struct Bar {
doSomething(Bar<T>& other); // arguably good
doSomething(Bar& other); // arguably bad
};
Run Code Online (Sandbox Code Playgroud)
问题是,没有人可以用严格的事实来支持他们的主张,它只是" 名称查找可能出错 "而不是" meh,从来没有任何问题 ".
为了解决这个问题:这两个约定是否严格等同,或者"坏"有时会在名称查找中引入含糊之处?参考现行标准将是非常好的.
当然,不应该考虑易读性的论点,我真的在询问符合标准的编译器在极端情况下的表现.但是,已知的实现错误也是受欢迎的.
c++ fully-qualified-naming language-lawyer explicit-specialization
我这里有代码,
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
//checking some conditions here for true or false
if(false)
{
break out of this for loop;
}
else if(true)
{
printf("true");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想打破内部for循环并继续外循环.我尝试使用,break
但控件也移出了父for循环.
对此有何解决方案?
#include<iostream>
#include<conio.h>
int a[9][9], b[9][9];
int inputvalue(int x, int y, int value)
{
for (int i = 0; i < 9; i++) {
if (value == b[x][i] || value == b[i][y])
return 0;
}
for (i = (x / 3) * 3; i <= ((x / 3) * 3) + 2; i++)
for (int j = (y / 3) * 3; j <= ((y / 3) * 3) + 2; j++)
if (b[i][j] == value)
return 0;
return value;
}
Run Code Online (Sandbox Code Playgroud)