在找到关于stackoverflow的许多问题的答案之后,我现在遇到了一个我无法找到答案的问题,我希望有人愿意帮助我!
我的问题是我想在C++中对一个类中的函数进行明确的模板化.我的编译器(g ++)和C++标准(§14.7.3)中的一个看起来告诉我,这个特化必须在声明类的命名空间中完成.我明白这意味着我不能把专业化放在课堂里,但是我没有看到这个限制的重点!有没有人知道是否有充分的理由不让专业在课堂上进行?
我知道有一些解决方法,例如将函数放在结构体中,但我想理解为什么语言有这种设计.如果有充分的理由不在课堂上允许专门的功能,我想在尝试解决它之前我应该知道它.
提前致谢!
为了让我的问题更加精确:以下是一些测试示例中的代码,说明了我想要做的事情:
#include <cstdio>
namespace MalinTester {
template <size_t DIMENSIONALITY>
class SpecializationTest {
public:
SpecializationTest() {
privateVariable = 5;
};
virtual ~SpecializationTest() {};
void execute() {
execute<DIMENSIONALITY>();
};
private:
int privateVariable;
template <size_t currentDim>
static void execute() {
printf("This is the general case. Current dim is %d. The private variable is %d.\n", currentDim, privateVariable);
execute<currentDim-1>();
}
template <>
static void execute<0>() {
printf("This is the base case. Current dim is 0.\n");
}
};
Run Code Online (Sandbox Code Playgroud)
这是不可能的; g ++说: …
我正在尝试使用显式类型参数和约束来定义运算符:
let inline (===)<'a, 'b
when 'a : not struct
and 'b : not struct> a b = obj.ReferenceEquals (a,b)
Run Code Online (Sandbox Code Playgroud)
它在F#2.0中运行良好,但产生:
警告FS1189:
类型参数必须直接放在类型名称旁边,例如"type C <'T>",而不是类型"C <'T>"
那么为运算符定义做出显式类型参数规范的正确方法是什么?
ps请不要告诉我隐式类型参数和其他一些解决方法,我想具体解决这个问题.
我正在玩新explicit
的铸造操作员.如果你写的东西像
struct Data {
explicit operator string();
};
Run Code Online (Sandbox Code Playgroud)
不可能意外转换Data
为string
.所述darget数据类型bool
是一个例外:在某些情况下,隐式转换是允许即使它被标记explicit
- 上下文转换.因此,您可以在if(...)
以下示例中使用此数据类型:
struct Ok {
explicit operator bool(); // allowed in if(...) anyway
};
Run Code Online (Sandbox Code Playgroud)
该段"25.4.(2)分类及相关业务"似乎让本作Compare
的仿标箱状set
为好.但是我对gcc-4.7.0的尝试失败了,我注意到这是我的错误理解还是gcc中的错误?
#include <set>
struct YesNo { // Return value type of Comperator
int val_;
explicit YesNo(int y) : val_{y} {}
/* explicit */ operator bool() { return val_!=0; }
};
static const YesNo yes{1};
static const YesNo …
Run Code Online (Sandbox Code Playgroud) 根据标准的Android文档,启动服务(启动服务)的首选方法是使用如下的显式意图:
// Using explicit intent:
Intent serviceIntent = new Intent(getApplicationContext(), MyService.class);
// or:
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)
您还可以使用带有清单中指定的操作字符串的隐式intent来启动/停止服务,如下所示:
// Using implicit intent:
static final String serviceAction = "com.example.my.app.services.MYSERVICE";
Intent serviceIntent = new Intent(serviceAction);
startService(serviceIntent);
// AndroidManifest.xml:
<service android:name="com.example.my.app.services.MyService"
android:exported="false" android:process=":services" >
<intent-filter>
<!-- Start/Stop service -->
<action android:name="com.example.my.app.services.MYSERVICE" />
</intent-filter>
</service>
Run Code Online (Sandbox Code Playgroud)
当服务仅在本地使用时(不允许第三方应用程序启动或绑定它),文档说明您不应在清单服务标记中包含intent-filter,并且应将导出的标记设置为false.
注意:活动和服务在不同的进程(:application和:services进程)中运行.活动和服务之间的通信是通过实现AIDL接口完成的(这样做是因为只有AIDL远程接口允许我在需要同时处理IPC的服务中进行多线程,不仅在活动之间,而且主要在以下运行的服务之间进行:服务流程).
我的问题是:
问题1:当我在我的应用程序中使用的活动和服务在两个不同的进程中运行时,我是否需要使用隐式意图而不是显式意图来启动和停止服务?
Q2:当:应用程序进程消失(销毁,不再在内存中)并且:服务进程在后台运行时,如何再次从新的:应用程序进程连接到已运行的:services进程?不知何故,我需要再次获得对:services进程的引用,以便我可以在该进程中停止正在运行的服务.使用AIDL afaik无法做到这一点.
问题是Android可以并且将在资源不足时轻松破坏:应用程序进程,只要:services进程继续运行,我就可以了.(是的,我知道通过将服务设置为前台服务等来影响流程,我也可以阅读手册;)但这不是我的问题).
当活动和服务处于分离的进程中并使用AIDL时,我无法找到与我的问题相关的任何信息或答案,并且当:应用程序进程需要在Android被杀死之后再次"找到":服务进程时用户再次进入应用程序(之后他/她离开应用程序).
欢迎任何专家级的建议.
我试图理解c ++中显式关键字的用法,并在SO上查看了这个问题. 显式关键字在C++中意味着什么?
但是,那里列出的例子(实际上都是前两个答案)对于使用情况不是很清楚.例如,
// classes example
#include <iostream>
using namespace std;
class String {
public:
explicit String(int n); // allocate n bytes to the String object
String(const char *p); // initializes object with char *p
};
String::String(int n)
{
cout<<"Entered int section";
}
String::String(const char *p)
{
cout<<"Entered char section";
}
int main () {
String mystring('x');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我已经将String构造函数声明为显式,但是如果我不将它列为显式,如果我将构造函数称为,
String mystring('x');
Run Code Online (Sandbox Code Playgroud)
要么
String mystring = 'x';
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都会进入int部分.除非我指定值的类型,否则它默认为int.即使我使用参数更具体,例如将一个声明为int,另一个声明为double,而不是使用显式构造函数名称并以这种方式调用它
String mystring(2.5);
Run Code Online (Sandbox Code Playgroud)
或者这样
String mystring = 2.5;
Run Code Online (Sandbox Code Playgroud)
它总是默认为带有double参数的构造函数.所以,我很难理解显式的真实用法.你能给我一个例子吗?不使用显式将是一个真正的失败?
我正在使用转发构造函数创建一个瘦派生类.(请耐心等待,我必须使用缺少继承构造函数的GCC 4.7.2).
在第一次尝试时,我忘了添加explicit
关键字并出错.有人可以解释为什么会发生这种特殊错误吗?我无法搞清楚事件的顺序.
#include <memory>
template<typename T>
struct shared_ptr : std::shared_ptr<T>
{
template<typename...Args>
/*explicit*/ shared_ptr(Args &&... args)
: std::shared_ptr<T>(std::forward<Args>(args)...)
{}
};
struct A {};
struct ConvertsToPtr
{
shared_ptr<A> ptr = shared_ptr<A>(new A());
operator shared_ptr<A> const &() const { return ptr; }
};
int main()
{
shared_ptr<A> ptr;
ptr = ConvertsToPtr(); // error here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
test.cpp: In function ‘int main()’:
test.cpp:28:23: error: ambiguous overload for ‘operator=’ in ‘ptr = ConvertsToPtr()’
test.cpp:28:23: note: candidates are:
test.cpp:9:8: note: …
Run Code Online (Sandbox Code Playgroud) 有一个同事问我这个问题,在我脑子里迷茫的状态我没有答案:
你为什么这样做:
string ham = "ham " + 4;
Run Code Online (Sandbox Code Playgroud)
但不是:
string ham = 4;
Run Code Online (Sandbox Code Playgroud)
如果在连接时存在字符串转换的隐式转换/操作,为什么在将其指定为字符串时不一样?(当然,没有做一些运算符重载)
我在标准C++库中观察到以下向量构造函数
explicit vector(size_type n);
vector(size_type n, const T& value, const Allocator& = Allocator());
Run Code Online (Sandbox Code Playgroud)
有没有理由为什么没有标记第二个构造函数explicit
?这编译,让我感觉很糟糕
void f(vector<string>);
int main() {
f({10, "foo"});
}
Run Code Online (Sandbox Code Playgroud)
如果我省略了"foo"
它,它就不会编译,这就是我将int和string的一对(复合)值传递给想要一个字符串向量的函数时所期望的.
有谁知道为什么Variable not defined
我编译它时不会抛出错误?
'Class1.cls'
Option Explicit
Public Sub foo()
ReDim fubar(1 To 2, 1 To 1)
End Sub
Run Code Online (Sandbox Code Playgroud)
我误解了Option Explicit
应该如何运作?或者这个测试有问题吗?或者这只是VBA中的一个错误?
(我在Excel 2007上测试这个)
假设我有一个FunctionWrapper
像这样定义的类:
struct FunctionWrapper
{
FunctionWrapper(std::function<void()> f);
// ... plus other members irrelevant to the question
};
Run Code Online (Sandbox Code Playgroud)
我想,以防止隐式转换std::function<void()>
到FunctionWrapper
,但允许构建FunctionWrapper
使用括号初始化语法(即使用初始化列表用一个参数).换句话说,我想这样:
void foo();
void wrap(FunctionWrapper);
wrap(foo); // (1) error
wrap({foo}); // (2) OK
wrap(FunctionWrapper{foo}); // (3) OK
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这一目标?我上面定义类的方法不是它:这允许隐式转换,因此(1)编译.
如果我添加explicit
到构造函数:
struct FunctionWrapper
{
explicit FunctionWrapper(std::function<void()> f);
// ... plus other members irrelevant to the question
};
Run Code Online (Sandbox Code Playgroud)
它也没有帮助,因为它"太过分"并且不允许(2)以及(1).
有没有办法实现"中间地带"并且(2)编译而(1)产生错误?