alignas(T) not resolved in template function - c++

When use Eclipse with MinGW(version:4.8.1) to compile the following code fragement, it can pass but Eclipse still report:
"Multiple markers at this line - Type 'alignas' could not be resolved"
template<typename X> void set_aside(std::vector<X> vx) {
constexpr int max_buf = 1024;
alignas(X) X buffer[max_buf];
int max = min(vx.size(), max_buf / sizeof(X));
std::uninitialized_copy(vx.begin(), vx.begin() + max, buffer);
}
What happend with this issue, although the code fragement pass the comiple,yet Eclipse marks with error.
Someone have ever met this issue? Please kindly help me to resolve this issue,thanks very indeed!

Many IDEs use a front-end syntax checker that is different than their back-end compiler. Eclipse Kepler (released June 2013) is mostly up-to-date with C++11 syntax, although some things like alignment support and the interaction with in-class initializers and default constructors might not be fully supported (yet). Similary, C++14 features like decltype(auto) will work if the back-end compiler is called with std=C++1y but will not be recognized by the front-end syntax checker.
NOTE: this is not unique to Eclipse, also Visual C++ Intellisense is sometimes running behind (especially in CTP versions) the actual compiler, causing the red squiggly lines.

Related

Weird C++ syntax that Visual C++ 2022 understands but C++Builder rejects

I'm updating a huge legacy system, and am currently stumped by the following function:
template <class EL>
EL& FastInsertVector<EL>::operator[](size_type index)
{
return (EL&) FastInsertVectorBase::operator [][](index);
}
MS VC++ 2022 compiles it happily, whereas C++ Builder 11.1.5 rejects it saying:
expected expression
If I remove the second set of [], both compilers are happy. Unfortunately, this is shared code that both compilers must be able to handle.
This codebase is huge, and has no tests, and I'm having difficulty actually understanding what this syntax even means.
Is this valid C++ syntax? Can anyone explain what it's trying to express?

Enable visual studio c++ syntax error warnings when using template

I'm using visual studio 2017 and I noticed that I don't get syntax error warnings when using template, for example:
and when I remove the template I get this:
and this:
Is it possible to fix this?
code(not image):
template<int a>
void noErrors()
{
sleiudbg;sg ojrp jabp srpghs //some gibberish
}
template<int b>
void noErrors(string s)
{
int p = s.Size();
}
Oh please, must we post code only as images? Please fix that, it means I cannot experiment with your code.
But the answer to your question is that the incremental syntax checker built into the IDE is of limited intelligence, especially where templates are concerned (where detecting and reporting compiler errors in general is a complex business) and as a result a great deal of stuff which is not going to compile slips through.
The compiler, on the other hand, will throw this out straightaway, and also report errors resulting from an invalid instantiation of a template in a moderately helpful way (although such error messages are never the easiest things to read).
And can you change any of this? Sorry, no, (unless you can persuade Microsoft to beef-up their IDE).
where error checking is a complex business - more here:
What does a compiler check for uninstantiated template code?

Trying out [[nodiscard]] with no success

I am trying to test a little C++17. I am trying to do:
[[nodiscard]] int get_value1()
{
return 42;
}
inline void start()
{
// Should generate warning
get_value1();
}
However, it does not. I am using Visual studio 2017. Warning level set to Level4(\W4). I have set C++ Language Standard to ISO C++ Latest Draft Standard (/std:c++latest).
But it does not generate the warning that I want. Why is that? Also, a little side question: That tab to select the language standard only appears in Debug configuration and not Release. Why is that? Release complains on the nodiscard, Would that mean that Release is in C++14?
edit: naturally I meant warning in the second section. Corrected. :)
Actually [[nodiscard]] is supported only since VS 2017.3 and it should give you a warning, not an error. And as I understand specification assumes that compiler may warn you. And may not.

Why to use MOCK_NON_CONST_METHOD_EXT for operator float() in Boost Turtle?

I am using Boost 1.46 with Turtle lib 1.2.4 and compiler from Visual Studio Express 2013. I have following class to MOCK:
struct IPredicate
{
virtual ~IPredicate() {}
virtual bool operator()(float value) = 0;
};
When I mock operator() with MOCK_NON_CONST_METHOD:
MOCK_BASE_CLASS(MockPredicate, IPredicate)
{
MOCK_NON_CONST_METHOD(operator(), 1, bool(float), id)
};
I got bunch of compiler errors, e.g. syntax error 'operator ' and so on. But when I mock it with MOCK_NON_CONST_METHOD_EXT:
MOCK_BASE_CLASS(MockPredicate, IPredicate)
{
MOCK_NON_CONST_METHOD_EXT(operator(), 1, bool(float), id)
};
everything is ok and works perfectly! According to http://turtle.sourceforge.net/turtle/reference.html MOCKS with EXT suffix are for "compilers without support for variadic macros", but the one I am using has support (checked it with these examples: http://msdn.microsoft.com/en-us/library/ms177415.aspx ). The rest of the documentation isn't really clear about this case.
Is anyone able to explain me what's the case here? Why I have the errors when I don't use EXT suffixed MOCK version?
The stickler’s answer would be that generally there are no guarantees with respect to variadic macros, as variadic macros are non-standard in C++03 (but are standard in C++11). So if you have a method which avoids variadic macros you should definitely use it instead the one with the variadic macros.
Practically though it is very likely that the turtle library hasn’t been extensively tested with msc and is simply relying on one of the non-standard gcc extensions for the macros. The extensions are discussed on the Variadic Macros http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html page. Specifically, for the turtle library to be portable for all C99 conformant compilers only __VA_ARGS__ could be used.
With macros, when you are after the root case - use the /P switch for msc (Preprocess to a File) to generate an .i file with the pre-processors directives expanded, where you can check what's it unhappy about.
Update. As I finished spanning this long story, I decided to quickly download the turtle and check how the macro is defined. And as I did, I discovered that this is simply a sad case of unmaintained documentation. Running grep on the library includes I couldn't find MOCK_NON_CONST_METHOD defined at all. That's why you are getting syntax errors. Another reason to avoid macros - clarity and sanity of C++ error messages.
(I'm the author of turtle)
What happened with 1.2.4 is that for a reason I didn't really investigate the code provided is actually 1.2.1 along with the 1.2.4 documentation.
As nobody complained by opening a ticket directly on sourceforge I didn't notice until quite some time had passed (all my personal and company projects using turtle are continuously integrated with the latest source code).
Anyway, I just tested your code and it compiles with MSVC 2013, turtle 1.2.6 and boost 1.55. If you haven't already done so by the time, you should consider upgrading.

Which IDEs and text editors can deduce type of variables declared using auto keyword in C++11

In "Almost always auto" article Herb Sutter lists several reasons for declaring variables using auto keyword.
He says that actual variable type can be automatically deduced by IDE and shown by hovering over variable name.
I would like to know which IDEs and text editors (or plugins) currently support "auto" variable type deduction.
Edit:
List of IDEs from answers:
Visual Studio 201x
Eclipse
Qt Creator 2.7.0
KDevelop 4.5.1
Text editors
What about Vim, Emacs, Sublime Text, etc. - are there plugins which supports type deduction?
Visual Studio 2010, Visual Studio 2012, and Visual Studio 2013 support type deduction for variables declared with the auto keyword. This applies both to the IntelliSense tooltips as well as auto-complete suggestions.
Starting with Visual Studio 2010 the C++ IntelliSense support was completely reworked (see Rebuilding Intellisense). IntelliSense is now driven by the Edison Design Group (EDG) C++ compiler frontend. Whatever EDG can do you will see reflected in IntelliSense.
Note that IntelliSense tooltips will display the underlying type for auto variables. It will not work up the tree again and replace portions with appropriate typedefs. On Visual Studio 2012 the following code
std::string str;
std::string::iterator i1 = str.begin();
auto i2 = str.begin();
will display the iterators as
std::basic_string<char,std::char_traits<char>,std::allocator<char> >::iterator i1
and
std::_String_iterator<std::_String_val<std::_String_base_types<char,std::allocator<char> >::_Val_types>::_Myt> i2
Given that I would happily disagree with Herb Sutter on his assessment that an IDE is enough to deduce a type when you need it. auto is great with respect to robustness, correctness and flexibility, but it surely fails to meet a developer's needs working on a large code base.
Native support
Visual Studio 2010+
Caveat: Does not behave terribly well with typedefs; see iinspectable's answer
KDevelop 4.5.1+
Caveat: Some incorrect deductions (e.g. float literals); see Johnny's answer
Qt Creator 2.7.0+
Eclipse (not sure if via plugin or native)
Support via plugins
Vim plugins:
YCM (since merging this pull request: https://github.com/Valloric/ycmd/pull/88)
others? Syntastic, maybe?
Emacs plugins:
....SURELY there is such a plugin somewhere.
Note: the first draft of this answer was created by simply combining the existing answers plus the Eclipse comment, then adding a note about Vim. Without such an "aggregate" answer, this question (and its existing answers) appears to violate the "one right answer" rule.
This should really be formatted as a table; too bad we don't have that capability here.
KDevelop 4.5.1 also supports type deduction. Although it probably does small mistakes.
Examples:
auto i = 3; // Deduces int
auto d = 3.0; // Deduces double
auto f = 3.0f; // Deduces double - wrong
Qt Creator 2.7.0 can do it too, judging from this testing source code:
class A
{
void f();
};
int main()
{
auto a = A();
return 0;
}